P
P
Pavel2018-12-03 11:40:31
Laravel
Pavel, 2018-12-03 11:40:31

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.

Request
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',
        ];
    }
}

Controller
class MyController extends Controller
{
    public function create(CreateOrUpdateRequest $request)
    {
        dd($request->all());
    }
}

Well, the result
5c04eba87f5ff141334094.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2018-12-03
@Palych_tw

https://laravel.com/api/5.7/Illuminate/Foundation/...
Add a failedValidation() method to the file to handle errors.
---
Maybe this article will help.

J
JhaoDa, 2018-12-03
@JhaoDa

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 question

Ask a Question

731 491 924 answers to any question