Laravel 5.5验证失败时响应的验证更改格式 [英] Laravel 5.5 Validation change format of response when validation fails

查看:90
本文介绍了Laravel 5.5验证失败时响应的验证更改格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.4中,我们创建了一个继承所有验证请求的类,因为我们需要自定义响应.

In Laravel 5.4, we created a class that all our requests for validation inherited because we needed to customize our response.

class APIRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Response on failure
     * 
     * @param array $errors
     * @return Response
     */
    public function response(array $errors) {
        $response = new ResponseObject();

        $response->code = ResponseObject::BAD_REQUEST;
        $response->status = ResponseObject::FAILED;
        foreach ($errors as $item) {
            array_push($response->messages, $item);
        }
        return Response::json($response);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

一个可以扩展此要求的示例请求如下所示

A sample request that would extend this is shown below

class ResultsGetTermsRequest extends APIRequest
{
    /**
     * 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 [
            'school_id' => 'required|integer',
            'student_id' => 'required|integer',
        ];
    }
}

然后我们对失败的样本响应将是

And then our sample response on failure would be

{
    "status": "FAILED",
    "code": "400",
    "messages": [
        [
            "The school id field is required."
        ],
        [
            "The student id field is required."
        ]
    ],
    "result": []
}

但是,这不适用于Laravel 5.5.我注意到它们用failedValidation替换为响应方法.但是,如果请求未通过验证,则不会返回任何响应.如果取消注释print_r,则返回某些内容.似乎永远不会执行的唯一一行是return语句.我想念什么?

However, this doesn't work anymore with Laravel 5.5. I noticed they replaced with response method with failedValidation. This however isn't returning any response when the request isn't validated. If I un-comment the print_r, it is something is returned. It seems the only line that is never executed is the return statement. What am I missing?

 public function failedValidation(Validator $validator) {

        $errors = (new ValidationException($validator))->errors();
        $response = new ResponseObject();

        $response->code = ResponseObject::BAD_REQUEST;
        $response->status = ResponseObject::FAILED;
        foreach ($errors as $item) {
            array_push($response->messages, $item);
        }
        //print_r($response);
        return Response::json($response);
    }

推荐答案

我猜是按照laravel 升级指南,我们应该返回HttpResponseException

I guess as per laravel upgrade guide we should return HttpResponseException

protected function failedValidation(Validator $validator)
{
    $errors = $validator->errors();
        $response = new ResponseObject();

        $response->code = ResponseObject::BAD_REQUEST;
        $response->status = ResponseObject::FAILED;
        foreach ($errors as $item) {
            array_push($response->messages, $item);
        }

    throw new HttpResponseException(response()->json($response));
}

这篇关于Laravel 5.5验证失败时响应的验证更改格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆