脚本宝典收集整理的这篇文章主要介绍了php – 仅当另一个字段具有值时必填字段,否则必须为空,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个输入a和b. a是具有三个可能值的选择输入:x,y和z.我想写这条规则:
有没有办法写这样的规则?我试过@L_360_2@_wITh,required_without,但它似乎无法涵盖我的情况.
换句话说,如果前面的解释不够清楚:
编辑app / PRoviders / AppServiceProvider.PHP并将此验证规则添加到引导方法:
// Extends the validator \Validator::extendImplicit( 'empty_if',function ($attribute,$value,$parameters,$validator) { $data = request()->input($parameters[0]); $parameters_values = array_slice($parameters,1); foreach ($parameters_values as $parameter_value) { if ($data == $parameter_value && !empty($value)) { return false; } } return true; }); // (optional) Display error replacement \Validator::replACER( 'empty_if',function ($message,$attribute,$rule,$parameters) { return str_replace( [':other',':value'],[$parameters[0],request()->input($parameters[0])],$message ); });
(可选)在resources / lang / en / validation.PHP中创建错误消息:
'empty_if' => 'The :attribute field must be empty when :other is :value.',
然后在控制器中使用此规则(使用require_if来遵守原始帖子的两个规则):
$attributes = request()->validate([ 'a' => 'required','b' => 'required_if:a,x|empty_if:a,y,z' ]);
有用!
以上是脚本宝典为你收集整理的php – 仅当另一个字段具有值时必填字段,否则必须为空全部内容,希望文章能够帮你解决php – 仅当另一个字段具有值时必填字段,否则必须为空所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。