Overwriting Laravel Form Request Response

Feb 17, 20231 min read

To return custom response from FormRequest, you need to overwrite the failedValidation method from the FormRequest class

Code snippet:

...

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;

class CustomFormRequest extends FormRequest
{

    ...

    public function failedValidation(Validator $validator) {
        // JSON response, add your logic here.
        throw new HttpResponseException(response()->json([
            'data' => $validator->errors()
        ], 422));
    }
}