dongyi9783 2015-05-03 23:57
浏览 34
已采纳

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.  
}

I would use this rule like this

'my_field' => 'required|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 ?

  • 写回答

3条回答 默认 最新

  • dro62273 2015-05-04 03:49
    关注

    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');
    

    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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部