【问题标题】:Laravel Api does not send required error json message from Request classLaravel Api 未从请求类发送所需的错误 json 消息
【发布时间】:2020-02-26 08:29:34
【问题描述】:

请求类

class LoginRequest extends FormRequest
{
    public function wantsJson() {
        return true;
    }   

    public function authorize() {
        return true;
    }

    public function rules() {
        $validators = [
            'email'    =>  'required',
            'password' =>  'required'
        ];
        return $validators;
    }

    public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator) {
        if($validator->fails()) {
            //print_r($validator->errors());
            //die();

        }
        return parent::failedValidation($validator);
    }
}

我有一个用 Laravel 编写的 api。我正在尝试通过 Postman 扩展来测试验证。当我提交一些电子邮件和密码值时,它可以工作。我收到凭据是否存在的消息。

如果我不提交值,那么就没有返回 json messagebag。

我可以确认 MessageBag 中有验证错误消息。这是屏幕截图。如果截图看不清楚,请点击查看。

另外一个奇怪的是返回的状态码是200

如果您需要更多信息,请告诉我

【问题讨论】:

    标签: laravel laravel-5.7 laravel-5.8


    【解决方案1】:

    在我的情况下,我像这样设置我的 Laravel API。

    在我的App\Exceptions\Handler

    public function render($request, Exception $exception)
        {
            // return parent::render($request, $exception);
    
            $rendered = parent::render($request, $exception);
    
            if ($exception instanceof ValidationException) {
                $json = [
                    'error' => $exception->validator->errors(),
                    'status_code' => $rendered->getStatusCode()
                ];
            } elseif ($exception instanceof AuthorizationException) {
                $json = [
                    'error' => 'You are not allowed to do this action.',
                    'status_code' => 403
                ];
            }
            else {
                // Default to vague error to avoid revealing sensitive information
                $json = [
                    'error' => (app()->environment() !== 'production')
                        ? $exception->getMessage()
                        : 'An error has occurred.',
                    'status_code' => $exception->getCode()
                ];
            }
    
            return response()->json($json, $rendered->getStatusCode());
        }
    

    在顶部也导入这个

    use Illuminate\Validation\ValidationException;
    use Illuminate\Auth\Access\AuthorizationException;
    

    有助于将错误格式化为 JSON 格式。

    我的LoginRequest 看起来像这样(简单)

    class LoginRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'email' => 'required|email',
                'password' => 'required'
            ];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多