phplaravellaravel-validationlaravel-6laravel-response

Laravel 6 Custom validation error 'class does not exist' when overriding FormRequest's failedValidation method


I created a custom validation class located in app\Http\Requests:

<?php

namespace App\Http\Requests;

use App\Http\Requests\BaseFormRequest;

class RegisterUserRequest extends BaseFormRequest
{
    protected static $NEEDS_AUTHORIZATION = false;

    protected static $FORM_RULES = [
        'first_name'              => 'required|string|max:80',
        'last_name'               => 'required|string|max:80',
        'email'                   => 'required|unique:users|email',
        'password'                => 'required|string|min:6|confirmed',
        'terms_conditions'        => 'accepted'
    ];

}

and extends BaseFormRequest in the same directory:

<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exceptions\HttpResponseException;
//use Illuminate\Http\Request;

abstract class BaseFormRequest extends FormRequest 
{
    protected static $NEEDS_AUTHORIZATION = true;
    protected static $FORM_RULES          = [];
    protected static $ERROR_MESSAGES      = [];

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() : bool
    {
        return !static::$NEEDS_AUTHORIZATION;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() : array
    {
        return static::$FORM_RULES;
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
    */
    public function messages()
    {
        return static::$ERROR_MESSAGES;
    }


    /**
     * @overrride
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator) : void
    {
        //throw (new ValidationException(response()->json($validator->errors(), 422)));
        throw new HttpResponseException(response()->json($validator->errors(), 422));
    }

}

Now, when trying to use it in my controller:

<?php

namespace App\Http\Controllers\API;

use App\Services\UserService;
use Illuminate\Http\JsonResponse as JsonResponse;
//use Iluminate\Http\Request;
use App\Http\Requests\RegisterUserRequest;

class UserController extends Controller
{
    private $user;

    public function __construct(UserService $user)
    {
        $this->user = $user;
    }

    public function store(RegisterUserRequest $request) : JsonResponse
    {   
        $registered_user = $this->user->register($request->validated());

        return response()->json(['status' => 201, 'user_id' => $registered_user->id]);
    }

}

I'm getting the following error:

ReflectionException Class App\Http\Requests\RegisterUserRequest does not exist

The funny thing is that if I remove the overwritten method failedValidation the error is gone, although it redirects me to home page which is what I'm trying to avoid. I want to return always a json response.

Any suggestions? Thanks


Solution

  • Note that the failedValidation() function expects as parameter an instance of \Illuminate\Contracts\Validation\Validator:

    /**
    * ...
    * @param  \Illuminate\Contracts\Validation\Validator  $validator
    * ...
    */ 
    

    but you are passing a Illuminate\Validation\Validator instance:

    use Illuminate\Validation\Validator; 
    
    protected function failedValidation(Validator $validator) : void
    {
        throw new HttpResponseException(response()->json($validator->errors(), 422));
    }
    

    So, you can change

    use Illuminate\Validation\Validator; 
    

    to

    use Illuminate\Contracts\Validation\Validator; 
    

    or pass it with the namespace in the param:

    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator) : void
    {
        throw new HttpResponseException(response()->json($validator->errors(), 422));
    }