更改,错误响应在Laravel 5.4中显示单个错误消息

时间:2018-05-01 13:17:38

标签: laravel laravel-5.4

我正在使用 required_without 验证所需的任何一个字段。

return Validator::make($data, [
            'user_email' => 'required_without:user_phone|email',
            'user_phone' => 'required_without:user_email|min:10',
            'country_code' => 'required_with:user_phone',           
            'otp' => 'required|numeric|max:6',
        ]);

以及如何在我的tbl_users中注册或未注册的支票电子邮件或电话的用户唯一

我收到此错误消息,我想为 user_email user_phone 显示一行。 如果是user_phone,我正在使用country_code。

我收到了这些错误消息。

{
    "user_email": [
        "The user email field is required when user phone is not present."
    ],
    "user_phone": [
        "The user phone field is required when user email is not present."
    ]
}

我希望这种类型的错误消息带有s,如:

{ 
 "status":false,
 "message":"Please enter email or phone"
}

1 个答案:

答案 0 :(得分:0)

您可以使用第三个参数手动添加消息,删除required_without的{​​{1}}并保留user_email

user_phone

现在,您只会收到 $messages = [ 'user_phone.required_without' => 'Please enter email or phone', ] return Validator::make($data, [ 'user_email' => 'user_phone|email', 'user_phone' => 'required_without:user_email|min:10', 'country_code' => 'required_with:user_phone', 'otp' => 'required|numeric|max:6', ],$messages); user_phone

的错误消息