如何在Laravel中使用多个required_if?

脏手

1)我是laravel的新手,想集成验证规则。我的要求是在其他两个字段的基础上使第三字段成为强制性的。如果a和b均为true,则必填字段C。我已经使用required_if将验证放在其他单个字段的基础上,但是如何使用required_if检查两个字段?

2)为了实现上述功能,我也尝试了自定义验证规则。但是,只有在我将要求的规则与之配合时,它才起作用。

例如:

'number_users' => 'required|custom_rule'   //working 
'number_users' => 'custom_rule'   //Not working
Lukasgeiter

您可以为此使用条件规则

这是一个简单的例子:

$input = [
    'a' => true,
    'b' => true,
    'c' => ''
];

$rules = [
    'a' => 'required',
    'b' => 'required'
    // specify no rules for c, we'll do that below
];

$validator = Validator::make($input, $rules);

// now here's where the magic happens
$validator->sometimes('c', 'required', function($input){
    return ($input->a == true && $input->b == true);
});

dd($validator->passes()); // false in this case

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章