What's the best way to populate forms with database data made using the Form class in Laravel while still giving way to Input::old()
if there are any errors? I can't seem to get it right.
My current setup looks something like this
public function getSampleform() {
// Load database data here
return View::make('sampleform');
}
public function postSampleform() {
// Save to database again then redirect to success page
return Redirect::to('success');
}
I usually echo my fields in the View this way:
<?php echo Form::text('entry', Input::old('entry'), array('class' => 'form-select'); ?>
What am I doing wrong?
The best way to do this is using Form Model Binding (http://four.laravel.com/docs/html#form-model-binding):
Use an existing model or create an 'empty' model class:
class NoTable extends Eloquent {
protected $guarded = array();
}
Find your model or instantiate your empty class and fill it with data:
public function getSampleform() {
// Load database data here
$model = new NoTable;
$model->fill(['name' => 'antonio', 'amount' => 10]);
return View::make('sampleform')->with(compact('model'));
}
If you'll use your form with a table that you already have data on it, this is how you uset it:
public function getSampleform() {
// Locate the model and store it in a variable:
$model = User::find(1);
// Then you just pass it to your view:
return View::make('sampleform')->with(compact('model'));
}
To have your form populated, use Form Model Binding, this is an example in Blade:
{{ Form::model($model, array('route' => array('sample.form')) ) }}
{{ Form::text('name') }}
{{ Form::text('amount') }}
{{ Form::close() }}
You don't even have to pass your Input data, because Laravel will populate your inputs using what comes first:
1 - Session Flash Data (Old Input)
2 - Explicitly Passed Value (wich may be null or not)
3 - Model Attribute Data
And Laravel will also take care of csrf token for you, using Form::open() or Form::model().
The old()
helper in Laravel (at least in 5.0) allows for a default value so that if some default value $entry
is defined, then if you do this:
<?php
echo Form::text('entry', Input::old('entry', $entry), array('class' => 'form-select');
?>
The helper will first try to look up the old form value and failing that will use the value $entry
. This also avoids having to use a ternary operator in your code.
However, when doing the redirect and there is an error you have to re-bind the old input data so that your postSampleform()
method would look like:
public function postSampleform() {
// Save to database again then redirect to success page
if ($success)
{
return Redirect::to('success');
}
else
{
return Redirect::to('sampleform')->withInput(Request::all());
}
}
You have to pass old input from the controller ($entry
should contain your database entry):
return View::make('sampleform')->with('entry', $entry)->with_input();
And then in the view, use an inline if statement to load the input if present, or else load from database:
Form::text('entry', Input::old('entry') ? Input::old('entry') : $entry, array('class' => 'form-select');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With