Laravel扩展验证自定义消息 [英] Laravel Extended Validation custom message
本文介绍了Laravel扩展验证自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建此扩展验证.
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.
}
我会这样使用
'my_field' => 'required|my_custom_validation_rule'
,
我想对"my_custom_validation_rule
"的错误使用一些动态消息
I want to use some dynamic message for the error of "my_custom_validation_rule
"
我无法从文档中找到有关它的信息.反正有做吗?
I was unable to find something from the documentation about it. Is there anyway to do it ?
推荐答案
extend
方法允许将消息作为第三个参数传递:
The extend
method allows to pass the message as a third argument:
Validator::extend('my_custom_validation_rule', function ($attribute, $value, $parameters) {
// ...
}, 'my custom validation rule message');
默认情况下,您只能使用动态变量,即:attribute
.如果要添加更多内容,请使用Validator::replacer()
:
By default you can only use dynamic variable, which is :attribute
. If you want to add more use Validator::replacer()
:
Validator::replacer('my_custom_validation_rule', function($message, $attribute, $rule, $parameters){
return str_replace(':foo', $parameters[0], $message);
});
这篇关于Laravel扩展验证自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文