【问题标题】:Laravel : Validation messages for custom ruleLaravel:自定义规则的验证消息
【发布时间】:2019-01-09 17:54:03
【问题描述】:

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

$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 文档中找不到与此相关的任何内容。

【问题讨论】:

标签: php laravel laravel-5


【解决方案1】:

试试这个:

$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',
        ]
);

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 2021-08-30
    • 1970-01-01
    • 2011-04-29
    • 2023-03-20
    • 2018-04-30
    • 2013-12-17
    • 2019-03-16
    • 1970-01-01
    相关资源
    最近更新 更多