Laravel 扩展验证自定义消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30025252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:29:42  来源:igfitidea点击:

Laravel Extended Validation custom message

phplaravellaravel-validation

提问by Ajeesh Joshy

I wanted to create this extended validation.

我想创建这个扩展验证。

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
   // I guess I should be setting the error message for this here.(Its dynamic)
   // We can return true or false here depending upon our need.  
}

I would use this rule like this

我会像这样使用这个规则

'my_field' => 'required|my_custom_validation_rule',

'my_field' => 'required|my_custom_validation_rule',

I want to use some dynamic message for the error of "my_custom_validation_rule"

我想对“ my_custom_validation_rule”的错误使用一些动态消息

I was unable to find something from the documentation about it. Is there anyway to do it ?

我无法从文档中找到有关它的内容。反正有办法吗?

回答by lukasgeiter

The extendmethod allows to pass the message as a third argument:

extend方法允许将消息作为第三个参数传递:

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
    // ...
}, 'my custom validation rule message');

By default you can only use dynamic variable, which is :attribute. If you want to add more use Validator::replacer():

默认情况下,您只能使用动态变量,即:attribute. 如果您想添加更多使用Validator::replacer()

Validator::replacer('my_custom_validation_rule', function($message, $attribute, $rule, $parameters){
    return str_replace(':foo', $parameters[0], $message);
});

回答by ?lter Ka?an ?cal

You can also define the message for your custom validation rule under validation translations file.

您还可以在验证翻译文件下为您的自定义验证规则定义消息。

/resources/lang/en/validation.php

/resources/lang/en/validation.php

....
'unique'                    => 'The :attribute has already been taken.',
'uploaded'                  => 'The :attribute failed to upload.',
'url'                       => 'The :attribute format is invalid.',
//place your translation here
'my_custom_validation_rule' => 'The :attribute value fails custom validation.'

回答by rmdwirizki

This is basically the same way as @lukasgeiter answer, but in case you need to manage dynamic variable inside the extend function, you can use $validator->addReplacerinside the extend directly.

这与@lukasgeiter 的回答基本相同,但如果您需要在扩展函数内部管理动态变量,则可以$validator->addReplacer直接在扩展内部使用。

Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters, $validator) {

    // Test custom message
    $customMessage = request()->get('foo') 
        ? "Foo doesn't exist"
        : "Foo exist";

    // Replace dynamic variable :custom_message with $customMessage
    $validator->addReplacer('my_custom_validation_rule', 
        function($message, $attribute, $rule, $parameters) use ($customMessage) {
            return \str_replace(':custom_message', $customMessage, $message);
        }
    );

    // Test error message. (Make it always fail the validator)
    return false;

}, 'My custom validation rule message. :custom_message');

回答by fico7489

possible (not very elegant) workaround is :

可能(不是很优雅)的解决方法是:

$message = 'my custom validation rule message' . request()->get('param');
Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
    //
}, $message);