Laravel Back to page with old input for validation

Biz Dev B picture Biz Dev B · Feb 3, 2015 · Viewed 18.1k times · Source

For the Update Profile Page

I use the route as

Route::get('editdriver/{data}', 'DriverController@EditDriver');

And in the controller after validation i use,

return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());

So, the url will be

http://localhost/project/editdriver/1

If i empty the value which is required in the rule and press submit the form,

It shows the old data with the validation message.

What i need is, It should not show the old value, which is from the db.

I even tried.,

return Redirect::back()->withErrors($validation->messages());

How can i do this ??

Update :

In the controller i have

public function EditDriver($data=NULL)
    {
        $editvehicle=$data;
        $DriverDetails = DriverModel::where('DriverId', $editvehicle)->get()->toArray();

        return View::make('home/editdriver')->with('DriverDetails', $DriverDetails);
    }

In the view i have

$("#Firstname").val("<?php echo $DriverDetails[0]['Firstname']?>");

And displaying from jquery to html as

{{ Form::text('Firstname',null, array('id'=> 'Firstname')) }}

Answer

Laurence picture Laurence · Feb 3, 2015

I have no idea why you are using jQuery to fill in your form - that makes no sense and you should remove that code.

All you need is this one line of code:

{{ Form::text('Firstname', Input::old('Firstname', $DriverDetails[0]['Firstname']), array('id'=> 'Firstname')) }}

Using Input::old($DriverDetails[0]['Firstname']) will prefill the inital form with your database value - but will then fill in the form with any 'inputted' values if there is a validation error

And to confirm - your original return statement looks correct:

return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());