Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe82's avatar

How to prevent 422 console error with laravel validation

Is there any chance to prevent 422 console error with validation using form request? Everything works fine, I validate modal form with ajax but still receiving this annoying console error.

0 likes
8 replies
tykus's avatar

You can handle failed validation in the form request by overriding the failedValidation method.

But the console error is correct, you have gotten a 422 because the submitted form was unprocessable. I wouldn't stress about this error - 99.9% of users will not see the console.

ronaldjesse's avatar

How are you currently validating requests?

Is it through $request->validate()?

You could also do this manually if your controller extends laravel's basecontroller Illuminate\Routing\Controller by accessing the validation factory, (this gives you a little bit more control) like this:

$validator = $this->getValidationFactory()->make($request->all(), [
    'name' => 'required'
    // etc.
];

if(!$validator->passes()) {
    // Return JSON response on fail
}

// Continue as the validation passed.

1 like
ejdelmonico's avatar

Try doing a dd($request->all() to make sure you are posting the required data to the server. This will reduce the reasons why the request is unprocessable. Are you sending the token as well?

1 like
knubbe82's avatar

@RONALDJESSE - I use dedicate request for that with rules and custom messages. Everything works fine but I want to prevent 422 error even if I receive correct errors messages

1 like
tykus's avatar
tykus
Best Answer
Level 104

If you do not want errors in the console, then you will need to return a 2xx response status code. To achieve this, you need to do as I suggested and override the failedValidation method in the FormRequest class(es) and return a custom response, e.g.

public function failedValidation(Validator $validator)
{
    return response()->json($validator->errors());
}

Or, in app/Exceptions/Handler.php you can catch a ValidationException and response to all validation expections in the same manner.

1 like
tykus's avatar

Bear in mind if you decide to return a 2xx status code, your javascript HTTP client will not execute the error or catch callbacks for validation failures. Instead your success callback will need to check if it is a real success or a validation failed "success"

Please or to participate in this conversation.