1)我对Laravel还不熟悉,想整合验证规则。我的要求是在其他两个字段的基础上强制使用第三个字段。如果A和B都为真,则字段C是必需的。我使用了required_if在其他单个字段的基础上进行验证,但如何使用required_if检查两个字段?
2)为了实现上述功能,我还尝试了自定义验证规则。但只有当我把必要的规则拉出来的时候,它才起作用。
例如:

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

最佳答案

你可以用conditional rules来做。
下面是一个简单的例子:

$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

关于php - 如何在laravel中使用multi required_if?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29609822/

10-14 13:03
查看更多