php – Laravel中的自定义验证消息

php – Laravel中的自定义验证消息,第1张

概述我需要一条错误消息,基本上说“你需要在至少一个多下拉列表中检查至少一个方框” 我的五个多下拉名称是;国家,县,Gor,Locauth,Parlc. 到目前为止我的控制器是; $rules = Array( [country] => required_without_all:county,gor,locauth,parlc [county] => required_without_all: 我需要一条错误消息,基本上说“你需要在至少一个多下拉列表中检查至少一个方框”

我的五个多下拉名称是;国家,县,Gor,Locauth,Parlc.

到目前为止我的控制器是;

$rules = Array(  [country] => required_without_all:county,gor,locauth,parlc  [county] => required_without_all:country,parlc  [gor] => required_without_all:country,county,parlc  [locauth] => required_without_all:country,parlc  [parlc] => required_without_all:country,locauth)$valIDator = \ValIDator::make($input,$rules);

我的问题是我看不到只返回一条规则的方法.它返回了5个非常相似的措辞规则;

The country fIEld is required when none of county / gor / locauth / parlc are present.The county fIEld is required when none of country / gor / locauth / parlc are present.The gor fIEld is required when none of country / county / locauth / parlc are present.The locauth fIEld is required when none of country / county / gor / parlc are present.The parlc fIEld is required when none of country / county / gor / locauth are present.

不太棒!有没有办法只返回一个自定义消息?

—编辑—

我应该添加…上面的Array()代码不是实际的代码,它是创建这些规则的实际代码的print_r结果.我只是觉得它会让你更容易阅读和理解我的问题.实际的代码,如果你感兴趣的是这个;

$fIElds = ['country','county','gor','locauth','parlc'];    $rules = [];    foreach ($fIElds as $i => $fIEld) {        $rules[$fIEld] = 'required_without_all:' . implode(',',array_except($fIElds,$i));    }

—另一个编辑—

我已经知道自定义错误消息了;

$messages = ['required' => 'The :attribute fIEld is required.',];$valIDator = ValIDator::make($input,$rules,$messages);

但这只会给我五条错误信息,而不只是一条.

解决方法 您只需要在一个字段上使用该规则即可正常工作.最简单的方法是创建一个空字段并将规则应用于它.该字段不必存在于数据库模式中或应用程序中的任何其他位置.

public static $rules = [    location => required_without_all:country,parlc];

然后在您的语言文件中自定义消息.

'custom' => array(    'location' => array(        'required_without_all' => 'At least one location fIEld is required',),

现在,当所有字段(或您的案例中的复选框)都为空时,您将收到错误消息.如果填写了一个或多个,则不会有任何错误.

总结

以上是内存溢出为你收集整理的php – Laravel中的自定义验证消息全部内容,希望文章能够帮你解决php – Laravel中的自定义验证消息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1256193.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存