Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-populate form with old data if a validation error occurs?

Tags:

laravel

The problem here is that the country field is not being re-populated whenever I submit the form. What I expect is for the user to submit the form and have the server validate it and redirect back with the errors and keep the data that the user input before submitting.

For some unknown reason, the country field is not getting the old input but the state field is getting the old input perfectly.

<select id="country" name ="country" class="form-control rounded input-lg text-center no-border" required autofocus <option> </option> </select> </br> <select name ="state" id ="state" class="form-control rounded input-lg text-center no-border" required autofocus <option>{{ Request::old('state') }}</option> </select> </br>

like image 560
Tony Batista Avatar asked Dec 10 '22 14:12

Tony Batista


1 Answers

You can use just old() helper for that:

<input type="text" name="username" value="{{ old('username') }}">

Another way to do that is to use Laravel Collective form model binding:

{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}

In this case all form data will be re-populated automatically.

like image 125
Alexey Mezenin Avatar answered May 14 '23 09:05

Alexey Mezenin