Answer the question
In order to leave comments, you need to log in
In Laravel, how to return already filled data to the form if the input is incorrect?
I’ll clarify right away that the question concerns work without using the form helper, i.e. All forms are made by hand.
The question is about updating a record that already exists in the database, not adding it.
All manuals suggest this route system for this operation:
Route::get('article/{article}/edit', ['as' => 'article.edit', 'uses' => '[email protected]']) ;
Route::patch('article/{article}', ['as' => 'article.update', 'uses' => '[email protected]']);
If I understand everything correctly, then the edit method displays the form and fills it with the data of the fields of the record being edited from the database. When you click on "save" call the update method,
But what to do in a situation where incorrect data is entered (for example, it did not pass validation), in this case, you need to display the form again in such a way that it is filled with the "last" data entered by the user.
In the case of create, everything is simple - we use {{ old('...) }} to display the latest data.
But what about update? It turns out that the update method should save the data in case of successful input, or again display the form with the entered data {{ old('...) }}?
But in this case, it turns out that in fact we need three form templates - for create, for edit and for update. It turns out a lot.
Answer the question
In order to leave comments, you need to log in
If the validation fails, then the update method should redirect back to the edit route like this
and you can also use old()
Better use the request object
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question