Laravel:自定义规则的验证消息

时间:2019-01-09 09:43:29

标签: php laravel laravel-5

我正在尝试使用以下代码添加自定义验证消息,

$validator = Validator::make(
            $user,
            [
                'first_name' => 'required|min:2',
                'email'      => [
                    'required',
                    'email',
                    Rule::notIn(array_column(Customer::getEmails(), 'email'))
                ]
            ],
            ['email.required' => 'Email is required (Custom message)']);

我为email required验证添加了一条自定义消息。那里没有问题。

对于Rule::notIn验证,当前返回The email is invalid。在这种情况下,如何添加自定义消息?

在Laravel文档中找不到与此相关的任何内容。

1 个答案:

答案 0 :(得分:2)

尝试一下:

$validator = Validator::make(
        $user,
        [
            'first_name' => 'required|min:2',
            'email'      => [
                'required',
                'email',
                Rule::notIn(array_column(Customer::getEmails(), 'email'))
            ]
        ],
        [
            'email.required' => 'Email is required (Custom message)',
            'email.email' => 'Your custom message here',
            'email.not_in' => 'Your custom message here',
        ]
);