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

cu256's avatar
Level 1

How to make a custom FormRequest error response in Laravel 5.5

Hello,

I am using FormRequests to validate data, but want to return custom JSON on error. So far I was extending the FormRequest and overriding the response method. But this stopped working with the update to 5.5. What is the current intended way to return a custom response on error?

What I tried is to override the failedValidation method and put my response into a ValidationException, however in this case a ReflectionException is thrown, because my Request class is not found.

What is the correct way to create a custom response in laravel 5.5?

Laravel 5.4

abstract class ApiFormRequest extends FormRequest
{
    public function response( array $errors ) {
        $json = [
            'status' => 'input_error',
            'errors' => 'more data'
        ];
        return new JsonResponse( $json, 400 );
    }
}

What I tried

public function failedValidation(Validator $validator)
{
    $json = [
        'status' => 'input_error',
        'errors' => 'more data'
    ];
    $response = new JsonResponse( $json, 400 );
    throw (new ValidationException($validator, $response))->status(400);
}

But throws ReflectionException in line 25 in /var/www/vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php

"Class App\Http\Requests\MyApp\Api\Page1Request does not exist"

Page1Request extends ApiFormRequest and definitely exists, because it works when I comment out the override method.

0 likes
9 replies
cu256's avatar
Level 1

I did the obligatory composer dump autoload and artisan clear compiled which did not help with the ReflectionException.

Kimmo's avatar

I had same problem.

I solved it by adding to app/http/exceptions/Handler.php file render function following lines.

if ($exception instanceof \Illuminate\Validation\ValidationException) { return new JsonResponse($exception->errors(), 422); }

I dont know if this is correct way but seems to work.

2 likes
cu256's avatar
Level 1

Good idea.

The "old" way was more flexible and less centralized. But this definitely works.

clay's avatar

From the upgrade documentation:

A Note On Form Requests

If you were customizing the response format of an individual form request, you should now override the failedValidation method of that form request, and throw an HttpResponseException instance containing your custom response:

use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * Handle a failed validation attempt.
 *
 * @param  \Illuminate\Contracts\Validation\Validator  $validator
 * @return void
 *
 * @throws \Illuminate\Validation\ValidationException
 */
protected function failedValidation(Validator $validator)
{
    throw new HttpResponseException(response()->json(..., 422));
}
5 likes
cu256's avatar
Level 1

That is nice, however as written in OP I get a ReflectionException in RouteSignatureParameters.php that the inheriting request class does not exist.

E.g: MyExampleRequest extends MyApiRequest extends FormRequest

MyApiRequest overrides failedValidation

ReflectionException: MyExampleRequest does not exist in RouteSignatureParameters.php

Edit: Tried it with overriding in MyExampleRequest directly, same exception.

Kimmo's avatar

I also tried to override failedValidation method. Got same ReflectionException in RouteSignatureParameters. Could not get it to work no matter what i did. Had to think alternative way around it :)

Michaelisah's avatar
Level 1

Hey CU256,

I think this might help, after trying clay's approach, at first it didn't work (kept on getting the ReflectionException). That was because I was using the:

use Illuminate\Support\Facades\Validator;

So i changed it to:

use Illuminate\Contracts\Validation\Validator;

and my override method is:

protected function failedValidation(Validator $validator) { throw new HttpResponseException(response()->json($validator->errors(), 422)); }

Hope it helps.

25 likes
Shahrukh4's avatar

After the updation of laravel documentation, you don't need to override the response() anymore, all you have to do is, just write your bussiness logics inside the protected failedValidation() inside your custom FormRequest class like follows,

    use Illuminate\Http\Exceptions\HttpResponseException;
    use Illuminate\Contracts\Validation\Validator;

    /**
    * [failedValidation [Overriding the event validator for custom error response]]
    * @param  Validator $validator [description]
    * @return [object][object of various validation errors]
    */
    public function failedValidation(Validator $validator) { 
         //write your bussiness logic here otherwise it will give same old JSON response
        throw new HttpResponseException(response()->json($validator->errors(), 422)); 
    }
10 likes

Please or to participate in this conversation.