I have checked all similar questions, no one seem to have answered this.
$data = ['data' => array(
['id'=>'1','name'=>'Dupe', 'country' => 'Nigeria'],
['id'=>'3','name'=>'Dipo', 'country' => 'Togo']
)];
$mustache->render('index', $data);
index.html looks like this:
<div>Hello</div>
<br>
<h2>Search for a person</h2>
<form action="/search" method="POST">
Input user's name or email:
<br><input type="text" name="key" value="" required>
<br>
<input type="submit" value="Search">
</form>
{{ #data }}
<div> {{ name }} - {{ country }} </div>
{{ /data }}
It is currently returning a blank page.
I couldn't figure that out, but converting the inner array to objects works just fine:
$m = new Mustache_Engine();
$data = [
'data' => array(
['id' => '1', 'name' => 'Dupe', 'country' => 'Nigeria'],
['id' => '3', 'name' => 'Dipo', 'country' => 'Togo']
)
];
$data = json_decode(json_encode($data));
echo $m->render(
'{{#data}}
<div> {{ name }} - {{ country }} </div>
{{/data}}',
$data
);
This outputs:
<div> Dupe - Nigeria </div>
<div> Dipo - Togo </div>
Notice {{#data}} and {{/data}} have no spaces!
I've used an inline template string as i didn't have yours.
Related
I'm getting the error when I try to create a post and I don't know how to fix it since I just began using laravel and I'm still a noobie.
Route (web.php)
Route::put('/create_action', [App\Http\Controllers\ActionController::class, 'createAction'])->name('auditor.create_action');
This is my controller method:
public function createAction(Request $request) {
$sectors = \DB::table('sector')->get();
$risicosoorten = \DB::table('risicosoort')->get();
$risicoclassificaties = \DB::table('risicoclassificatie')->get();
$users = \DB::table('users')->where('name')->get();
$statussen = \DB::table('status')->get();
return view('auditor.create_action' , [
'sectors' => $sectors,
'risicosoorten' => $risicosoorten,
'risicoclassificaties' => $risicoclassificaties,
'users' => $users,
'statussen' => $statussen
]);
$actie->create([
'create_date' => $req->create_date,
'bron_detail' => $req->bron_detail,
'audit_oordel' => $req->audit_oordel,
'process' => $req->process,
'nummer_bevinding' => $req->nummer_bevinding,
'omschrijving_bevinding' => $req->omschrijving_bevinding,
'probleem' => $req->probleem,
'risico_beschrijving' => $req->risico_beschrijving,
'oorzaak' => $req->oorzaak,
'aanbeveling_ia' => $req->aanbeveling_ia,
'map' => $req->map,
'datum_deadline' => $req->datum_deadline,
'datum_bijgesteld' => $req->datum_bijgesteld,
'datum_gesloten' => $req->datum_gesloten,
'voortgang' => $req->voortgang,
'aantekeningen_ia' => $req->aantekeningen_ia,
'oordeel_ia' => $req->oordeel_ia,
'sector' => $req->sector,
'pr' => $req->pr,
'sr' => $req->sr,
'arc' => $req->arc,
'orc' => $req->orc,
'grc' => $req->grc,
'status' => $req->status,
'sub_status' => $req->sub_status
]);
}
my blade file with the form:
#extends('layouts.master')
#section('title')
#endsection
#section('content')
<div class="create_action" id="post">
<form action="{{ route('auditor.create_action') }}" method="POST">
#method('PUT')
#csrf
<form>
<div class="form-group">
<label for="creation_date">Datum ontstaan actie</label>
<input type="date" class="form-control" name="create_date">
</div>
<div class="form-group">
<label for="bron_detail">Bron detail</label>
<input type="text" class="form-control" name="bron_detail">
</div>
<button type="submit" class="btn btn-primary" name="submitBtn" value="submitPost"><strong>Maak actie aan</strong></button>
#endsection
and wayy more form input fields but trying to keep it as short as possible!
You have two opening form tags in your view. Right after the #csrf directive. Is this intentional? Because you can not nest form tags.
This may be what is causing your error because the application is trying to use the 'Get' method for the inner form tag since no method was stated.
How can I control the number of Rows added to a textarea using the Illuminate\Html\FormFacade class?
I have added the field into my template.
<div class="form-group">
{!! Form::label('placeOfDeath','Place of Death') !!}
{!! Form::textarea('placeOfDeath',null,['class'=>'form-control']) !!}
</div>
When it gets rendered the textarea has cols="50" and rows="10"
<textarea class="form-control" name="placeOfDeath" cols="50" rows="10" id="placeOfDeath"></textarea>
I want a way to control these numbers, I have checked the documentation but couldnt spot anything?
The options (third parameter) array is actually the attributes array of that element, you so can just pass any 'key' => 'value' and the element will have it as attributes, for example:
{!! Form::textarea('placeOfDeath',null,['class'=>'form-control', 'rows' => 2, 'cols' => 40]) !!}
I have accepted the other answer as it works perfectly.
I have also found that the class actually checks for an attribute size
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
}
Its a minor space saving, Im not sure it makes the code anymore readable, but it is an alternative to cut off a few characters
['size' => '30x5']
Also try this:
{!! Form::textarea('placeOfDeath',null, array('class'=>'form-control',
'rows' => 10, 'cols' => 50)) !!}
This is my form:
{{ Form::model($data, array(
'route' => array('waitingtimes.update', $data->id),
'class' => 'mainInformationContrainer',
'method' => 'put'
)) }}
When I submit the form, I got
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
though I've already set the request as put.
Could you help me please?
Edit 1
I noticed that the form html is
<form method="POST" action="http://localhost:8082/test/public/waitingtimes/2" accept-charset="UTF-8" class="mainInformationContrainer">
</form>
It is post not put,
Edit 2
The problem was because I mistyped the route to rout, but not I am getting this exception
Trying to get property of non-object
this is the view:
{{Form::model($data, array(
'route' => array('waitingtimes.update', $data->id)
, 'class' => 'mainInformationContrainer',
'method' => 'put'
))}}
<ul>
<li>
<label>First Time:</label>
<div class="oneInfo">
{{ Form::text('startTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'startTime', 'autocomplete' => 'off'))}}
<span class="errorMessage">
<?php
echo $errors->first('startTime');
?>
</span>
</div>
</li>
<li>
<label>End Time:</label>
<div class="oneInfo">
{{Form::text('endTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'endTime'))}}
<span class="errorMessage">
<?php
echo $errors->first('endTime');
?>
</span>
</div>
</li>
<li>
<label>Value:</label>
<div class="oneInfo">
{{Form::text('value', $value=null, array())}}
<span class="errorMessage">
<?php
echo $errors->first('value');
?>
</span>
</div>
</li>
<li>
<input type="submit" value="Save Changes"/>
<input type="button" value="Cancle" class="cancelButton"/>
</li>
</ul>
{{ Form::close() }}
this is the controller update
$input = Input::all();
$validation = Validator::make($input, WaitingTimes::$rules);
if ($validation->passes()){}else{
return Redirect::route('waitingtimes.edit')->withInput()->withErrors($validation)->with(array(
'verticalMenu'=>'none',
'verticalMenuTab' => 'none',
'data' => $input
));
}
Please notice that this html blade code is used for editing the data and it is working correct when I call the edit function, and I am using it also to redirect when the user try to edit information but the validation falls
You'll need to specify the method in your form creation, add this to your Form::model array:
'method' => 'PUT'
You will need to tell your form that you will be using method PUT:
{{ Form::model($data, array(
'route' => array('waitingtimes.update', $data->id),
'class' => 'mainInformationContrainer',
'method' => 'put',
)) }}
Note that you will still see method = "POST" in your form but Laravel will add a hidden field called _method to your form. See http://laravel.com/docs/html#opening-a-form
I found the solution,
which is
return Redirect::back()->withInput()->withErrors($validation)->with(array(
'verticalMenu'=>'none',
'verticalMenuTab' => 'none',
'data' => $input
));
Thanks to this question
Laravel form model binding
Ok, so I am working on a test site while learning Laravel 4. This is my first MVC framework of any kind so I'm taking it slow.
I've already implemented several features including: Users (via Sentry), Profile (Users updating their own information), and all of that works fine.
Now, I am working on a system that allows users to request a quote for services. I felt like I understood the basic concepts behind forms and handling input, but apparently not. I was able to get the validation and everything out of the way, and all of that works. But now, when I go to actually execute the action (once the form data is validated), I am constantly given a blank page. I've tried pretty much everything.
Here is my QuoteController
public function create()
{
$user = Sentry::getUser();
if(!isset($statusMessage)) $statusMessage = "";
if(!isset($allErrors)) $allErrors = "";
return View::make('quote.create')->with(array('user' => Sentry::getUser(), 'statusMessage' => $statusMessage, 'allErrors' => $allErrors));
}
public function createDo()
{
$user = Sentry::getUser();
$input = Input::all();
$rules = array('quote_type' => 'required|in:web,design,other', 'budget' => 'required|in:1000,2000,5000,10000,20000,99999', 'sites_like' => 'max:5000', 'sites_dislike' => 'max:5000', 'content' => 'required|max:50000');
$filesrules = array('file' => 'mimes:jpeg,png,pdf,doc,txt,bmp,gif|max:500');
$validator = Validator::make($input, $rules);
if($validator->fails())
{
$allErrors = $validator->errors();
}
if(Input::hasFile('files'))
{
$files = Input::file('files');
if (!is_array($files))
{
$files = array($files);
}
foreach($files as $file)
{
if (!is_a($file, 'Symfony\Component\HttpFoundation\File\UploadedFile')) {
continue;
}
$fileValidator = Validator::make(array('file' => $file), $filesrules);
if($fileValidator->fails())
{
$messages = $fileValidator->messages()->toArray();
foreach($messages['file'] as $message)
{
$allErrors->add('file', $message);
}
}
}
}
if($allErrors->any())
{
Input::flash();
return Redirect::route('quote-create')->withErrors($allErrors)->withInput();
}
else
{
Quote::create(array(
'status' => 1,
'public_id' => strtoupper(substr(md5(rand(100000000,3888888888).time()), 0, 10)."-".substr(md5(rand(100000000,3888888888).time("a week ago")), 0, 10)),
'customer' => intval($user->id),
'quote_type' => $input['quote_type'],
'sites_like' => $input['sites_like'],
'sites_dislike' => $input['sites_dislike'],
'budget' => intval($input['budget']),
'content' => $input['content']));
return Redirect::route('quote');
}
}
And here's the routes
// Quote
Route::get('/quote', array('before' => 'auth|quote', 'uses' => 'QuoteController#index', 'as' => 'quote'));
Route::post('/quote/create', array('uses' => 'QuoteController#createDo', 'as' => 'quote-createDo'));
Route::get('/quote/create', array('before' => 'auth', 'uses' => 'QuoteController#create', 'as' => 'quote-create'));
// Staff
Route::get('/staff', array('before' => 'auth.staff', 'uses' => 'StaffController#index', 'as' => 'staff-index'));
// Sandbox
Route::get('/sandbox', array('before' => 'permission:user.create', function()
{
return "You have access!";
}));
And finally, the quote.create view.
{{ $errors }}
{{ $statusMessage }}
<br/><br/>
{{ Form::open(array('route' => 'quote-createDo', 'files' => true)) }}
<br/><br/>
<label for="quote_type"><span>Type of Quote</span></label>
{{ Form::select('quote_type', array('web' => 'Web', 'design' => 'Design', 'other' => 'Other')) }}
<br/><br/>
<label for="budget"><span>Project Budget</span></label>
{{ Form::select('budget', array('1000' => 'Up to $1,000', '2000' => '$1,000 - $2,000', '5000' => '$2,000 - $5,000', '10000' => '$5,000 - $10,000', '20000' => '$10,000 - $20,000', '99999' => 'Over $20,000')) }}
<br/><br/>
<label for="sites_like"><span>Three designs you like</span>List three websites or designs that you like, and why - in regards to design, formatting, graphics, anything. This will help us understand your tastes and give us a jumping-off point for your project.</label>
{{ Form::textarea('sites_like') }}
<br/><br/>
<label for="sites_dislike"><span>Three designs you don't like</span>Along the same lines, give us three examples of desgins or websites that you don't like.</label>
{{ Form::textarea('sites_dislike') }}
<br/><br/>
<label for="content"><span>The Meat</span>Explain to us, as best as you can, what you need us to do.</label>
{{ Form::textarea('content', '',array('class' => 'big')) }}
<br/><br/>
<label for="files[]"><span>File Upload</span>Upload any files here that might help us understand your needs better. (Diagrams, sketches, logos, etc.)</label>
{{ Form::file('files[]', array('multiple'=>true)) }}
<br/>
<br clear="all">
<br/><br/>
{{ Form::submit('Submit Quote', array('class' => 'submit')) }}
{{ Form::close() }}
Thank you very much for any and all help!
Joe
I am trying to add classes and ids to specific elements of a form in Laravel 4. For example, I would like this:
<textarea type="text" id="description" onfocus="this.value=''; setbg('#f0f7f8');" onblur="setbg('white')" name="description" value="" rows="10"></textarea>
to be applied to:
{{ Form::label('description', 'Description:') }}
{{ Form::textarea('description')}}
I didn't see this in the documentation. Thank you!
Use the third parameter for the Form::textarea method, passing a key-value array. Ex:
Form::textarea('description', null, [
'id' => 'description',
'rows' => 10,
]);
Although its an old question, I just wanted to say that you can escape the javascript like this:
Form::textarea('description', null, array(
'id' => 'description',
'rows' => 10,
'onFocus' => 'this.value=\'\'; setbg(\'#f0f7f8\');'
));
Thats it :)