When I use Validator class in Laravel I am able to catch errors like this for my ajax-
$validator= Validator::make($request->all(),[
'name' => 'required',
'email' => 'required|email',
'age' => 'required|min:3',
'number' => 'required|numeric',
]);
if($validator->fails()){
return response()->json([
'status'=> 400,
'errors'=>$validator->messages(),
]);
}
But how can I catch the error messages without using Validator class or using this code-
$request->validate([
'name' => 'required',
'email' => 'required|email',
'age' => 'required|min:3',
'number' => 'required|numeric',
]);
I want to store error messages in a variable in Controller so I can send them to JSON
CodePudding user response:
With the $errors var in the blade view Display Errors
CodePudding user response:
You need to do it manually, for example if you need to check that the user have entered email field you need to do this:
if(! $request->has('email')){
$error_email = "The email field is required".
}
Do this for each validation you want and then put them in an array and then send them as json.
I hope I've got your question correctly.
It if was correct, That's the only way.