【问题标题】:Laravel 5.2 - Create custom validation message when extending ValidatorLaravel 5.2 - 扩展验证器时创建自定义验证消息
【发布时间】:2016-06-16 16:36:06
【问题描述】:

在我的 AppServideProvider 的启动方法中,我为西里尔字母设置了自定义验证规则。这是它的样子:

public function boot()
{
    Validator::extend('cyrillic', function ($attribute, $value, $parameters, $validator) {
        return preg_match('/[А-Яа-яЁё]/u', $value);
    });
}

它按预期工作。但是,如果验证由于拉丁输入而未通过,我会收到错误消息“validation.cyrillic”。我该如何解决?如何将自定义消息传递给我的“西里尔文”验证规则?

【问题讨论】:

    标签: php validation laravel


    【解决方案1】:

    如果你想全局定义它,你需要编辑一个或多个验证文件,它位于resources/lang/LANG/validation.php,其中LANG是你想要定义的任何语言。

    例如,对于英文的,打开文件resources/lang/en/validation.php 并添加您的消息,如下所示。

    return [
        'accepted'             => 'The :attribute must be accepted.',
        'active_url'           => 'The :attribute is not a valid URL.',
        // Add yours to somewhere in the first level of the array
        'cyrillic'             => 'The :attribute is not Cyrillic.'
    ]
    

    对于本地,您可以在请求中定义它。

    public function messages()
    {
        return [
            'cyrillic' => 'The :attribute is not Cyrillic.'
        ];
    }
    

    【讨论】:

    • 工作就像一个魅力。干杯!
    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 2015-10-15
    • 2018-03-02
    • 2014-05-31
    • 2017-06-28
    • 2017-12-13
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多