Answer the question
In order to leave comments, you need to log in
How to correctly implement form validation in Laravel 6 when submitting it using AJAX?
Good afternoon. Help me understand the sequence of form validation when submitting a form via AJAX (laravel 6)
1. In AJAX, I take data from the form using and pass it on the form action.
2. This data is received in the controller and sent to the validator$('#form').serialize()
Validator::make($request->all(), [
// правила
])->validate();
return back()->with('message', 'Изменения сохранены.');
after validation in the controller. How to implement the code above in JS? (redirect to the initial and entry to the session). Answer the question
In order to leave comments, you need to log in
Read about form request, and remove Validator::make from the controller. After the main section of code, write
return response()->json(['message' => 'изменено'] ,200);
2.
public function update(Request $request, Post $post, $id)
{
$request->validate([
// rules
]);
// действия с БД
return response()->json([
'status' => true,
'message' => 'данные изменены'
]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question