如何在 Laravel 中使用多个 required_if?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29609822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:20:54  来源:igfitidea点击:

How to use multi required_if in laravel?

phplaravellaravel-5

提问by dirtyhandsphp

1) I am new to laravel and want to integrate validation rules. My requirement is to make third field mandatory on basis of two other fields. Field C is required if both a and b are true. I have used required_ifto put validation on basis of other single field but how can i use required_ifto check two fields?

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

2) To achieve above functionality i tried custom validation rule as well. But it's working only if i will pull required rule alongwith.

2)为了实现上述功能,我也尝试了自定义验证规则。但它只有在我将要求的规则一起拉出来时才有效。

For example:

例如:

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

回答by lukasgeiter

You can use conditional rulesfor that.

您可以为此使用条件规则

Here's a simple example:

这是一个简单的例子:

$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

回答by Pedro Papadópolis

You can also create a generic validator to test the AND operator.

您还可以创建一个通用验证器来测试 AND 运算符。

Validator::extend('required_multiple', function ($attribute, $value, $parameters, $validator) {
    $isRequired = true;

    for ($i = 0; $i <= count($parameters) / 2; $i = $i + 2) {
        $parameter = $parameters[$i];
        $value = $parameters[$i + 1];

        if (in_array($value, ['true', 'false'])) {
            $value = (bool)$value;
        }

        if ($validator->getData()[$parameter] !== $value) {
            $isRequired = false;
        }
    }

    if (!$isRequired || ($isRequired && $validator->getData()[$attribute])) {
        return true;
    } else {
        return false;
    }
});

Then in your controller rules:

然后在您的控制器规则中:

'phone_input_label' => 'required_multiple:goal,lead-capture,phone_input_active,true|string',

This will make "phone_input_label" required if goal is equal to "lead-capture" andphone_input_active is true.

如果目标等于“lead-capture”并且phone_input_active 为真,这将使“phone_input_label”成为必需。

The only problem with that custom validator is that you needto send all parameters. If "phone_input_label" is not sent in the request, it won't even pass through the custom validator, not sure why it happens.

该自定义验证器的唯一问题是您需要发送所有参数。如果请求中没有发送“phone_input_label”,它甚至不会通过自定义验证器,不确定为什么会发生。

回答by manix

Laravel evaluates each rule in the giver order. Let's say:

Laravel 会按照给出者的顺序评估每个规则。让我们说:

'number_users' => 'required|custom_a|custom_b'

custom_brule will be evaluate when requiredand custom_bare true because these rules were already evaluated.

custom_b规则将在何时被评估required并且custom_b为真,因为这些规则已经被评估。

回答by Vishal Wadhawan

Here is your answer

这是你的答案

 'field3' => 'required_if:field_1 | required_if:field_2'

回答by Azeame

Take a look at the laravel validation docs which I pasted a link below, you can use required_if, required_without and others to suit your needs.

看看我在下面粘贴了链接的 laravel 验证文档,您可以使用 required_if、required_without 和其他来满足您的需求。

See the Laravel Validation Docs Here

在此处查看 Laravel 验证文档