Laravel: filling out forms while maintaining the form :: old ()

What is the best way to populate forms with database data executed using the Form class in Laravel, still giving way to Input::old() if there are any errors? I don't seem to understand.

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 repeat my fields in a view like this:

 <?php echo Form::text('entry', Input::old('entry'), array('class' => 'form-select'); ?> 

What am I doing wrong?

+7
source share
4 answers

The best way to do this is to use model form 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 create an instance of 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 will use your form with a table on which you already have data, here is how you use 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 fill out your form, use model form 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, because Laravel will populate your inputs using the 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 the csrf token for you using Form :: open () or Form :: model ().

+12
source

You must pass the old input from the controller ( $entry must contain an entry in the database):

 return View::make('sampleform')->with('entry', $entry)->with_input(); 

And then in the view, use the inline if statement to load the input, if present, or load from the database:

 Form::text('entry', Input::old('entry') ? Input::old('entry') : $entry, array('class' => 'form-select'); 
+2
source

The old() helper in Laravel (at least in 5.0) accepts a default value, so if some default value $entry , then if you do this:

 <?php echo Form::text('entry', Input::old('entry', $entry), array('class' => 'form-select'); ?> 

The helper will first try to find the old form value and cannot use the value of $entry . It also avoids the use of the triple operator in your code.

However, when redirecting and an error occurs, you must re-bind the old input so that your postSampleform() method postSampleform() like this:

 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()); } } 
+2
source

I usually do this:

 // Check first if there is data from database else blank $entry = (isset($data->entry)) ? $data->entry : ''; <?php echo Form::text('entry', isset(Input::old('entry')) ? Input::old('entry') : $entry, array('class'=>'form-select')); ?> 

Then in your controller you can do this:

 public function getSampleform() { // Load database data $data = "Database data here"; return View::make('sampleform', compact('data')); } public function postSampleform() { // validate // if validation fails // redirect back and pass old inputs return Redirect::to('getSampleform')->withInput(); } 

Please note that this is for Laravel 4 .. Hope this works for you .. Greetings ...

0
source

All Articles