Answer the question
In order to leave comments, you need to log in
Why is FormRequest in Laravel throwing a ValidationException instead of an array with errors?
Hello. There is, for example, a post route described in api.php. There is a regular controller and a regular FormRequest. I send json with Postman and if the fields are invalid, then instead of the "field => error" array, an exception is thrown
Illuminate\Validation\ValidationException
The given data was invalid.
class CreateOrUpdateRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'firstName' => 'required|string|between:2,255',
'lastName' => 'required|string|between:2,255',
];
}
}
class MyController extends Controller
{
public function create(CreateOrUpdateRequest $request)
{
dd($request->all());
}
}
Answer the question
In order to leave comments, you need to log in
https://laravel.com/api/5.7/Illuminate/Foundation/...
Add a failedValidation() method to the file to handle errors.
---
Maybe this article will help.
Because that's the way it's designed. This exception should be caught by the error handler and it will already generate the desired response - json or redirect. What did you write there that he does not do this - the question is for you.
Do you not read the documentation for ideological or religious reasons?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question