简体   繁体   English

在laravel中以自定义格式返回验证器响应?

[英]return Validator response in custom format in laravel?

I am using validators as 我正在使用验证器作为

   $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
        'phone' => 'required|max:10|unique:users',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6',
    ]);

    if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);            
    }

this gives response in format below: 这给出以下格式的响应:

 {
    "error": {
        "phone": [
            "The phone has already been taken."
        ],
        "email": [
            "The email has already been taken."
        ]
    }
}

I want response as 我想要回应

{
"error": 1,
"error_msg": "The phone has already been taken."
}

Just one error should show and that too in above format. 上面的格式应该只显示一个错误。

For validation to stop after first validation failure, laravel use bail check this . 为了使验证在第一次验证失败后停止,laravel使用bail check this So the rules will be like this 所以规则会像这样

$validator = Validator::make($request->all(), [
    'name' => 'bail|required|max:255',
    'phone' => 'bail|required|max:10|unique:users',
    'email' => 'bail|required|email|max:255|unique:users',
    'password' => 'required|min:6',
]);

For formatting error message, you need to do some change in response 对于格式化错误消息,您需要在响应中进行一些更改

if ($validator->fails()) {
    return response()->json(['error'=> 1, 'error_msg' => $validator->errors()->first()], 400);           
}

You can create a central place to render the validation error so that it will be consistent validation response format through out the application. 您可以创建一个中心位置来呈现验证错误,以使其在整个应用程序中保持一致的验证响应格式。

Hope this may help you 希望这对您有帮助

Use a Form Request to handle the validation: 使用表单请求来处理验证:

class CreateUserRequest extends FormRequest {
     public function authorize()
     {
         return true;
     }

     public function rules()
     {
         return [
            'name' => 'required|max:255',
            'phone' => 'required|max:10|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6',
         ];
     }

     public function messages()
     {
         return [
            'phone.unique' => 'The phone has already been taken.',
            'email.unique'  => 'The email has already been taken.',
         ];
     }
}

Then use it in your controller: 然后在您的控制器中使用它:

public function create(CreateUserRequest $request)
{
     $user = User::create($request->all());

     return response()->json(['user'=>$user]);
}

This does the validation prior to running the create method and returns the validation error messages for you in a json response. 这会在运行create方法之前进行验证,并在json响应中为您返回验证错误消息。

I would suggest not changing the format of the response, instead adapt to the format returned from laravel as it will always be a uniform shape. 我建议不要更改响应的格式,而应适应laravel返回的格式,因为它始终是统一的形状。 That way your client can expect a consistent api response. 这样,您的客户端可以期望一致的api响应。

And as an aside, validation failures should be a 422 status, not a 401 which are for authentication purposes. 另外,验证失败应该是422状态,而不是用于身份验证的401状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM