gpt4 book ai didi

php - 覆盖 FormRequest 的 failedValidation 方法时,Laravel 6 自定义验证错误 'class does not exist'

转载 作者:行者123 更新时间:2023-12-02 14:47:11 26 4
gpt4 key购买 nike

我在 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'
];

}

并在同一目录中扩展 BaseFormRequest:

<?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));
}

}

现在,当尝试在我的 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]);
}

}

我收到以下错误:

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

有趣的是,如果我删除覆盖的方法 failedValidation 错误就消失了,尽管它会将我重定向到主页,这是我试图避免的。我想始终返回一个 json 响应。

有什么建议吗?谢谢

最佳答案

请注意,failedValidation() 函数需要一个 \Illuminate\Contracts\Validation\Validator 的实例作为参数:

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

但是你正在传递一个 Illuminate\Validation\Validator 实例:

use Illuminate\Validation\Validator; 

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

所以,你可以改变

use Illuminate\Validation\Validator; 

use Illuminate\Contracts\Validation\Validator; 

或者通过参数中的命名空间传递它:

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

关于php - 覆盖 FormRequest 的 failedValidation 方法时,Laravel 6 自定义验证错误 'class does not exist',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58367829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com