简体   繁体   English

如何在 Laravel 中使用多个 required_if?

[英]How to use multi required_if in laravel?

1) I am new to laravel and want to integrate validation rules. 1)我是laravel的新手,想集成验证规则。 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.如果 a 和 b 都为真,则字段 C 是必需的。 I have used required_if to put validation on basis of other single field but how can i use required_if to check two fields?我已经使用required_if在其他单个字段的基础上进行验证,但是如何使用required_if来检查两个字段?

2) To achieve above functionality i tried custom validation rule as well. 2)为了实现上述功能,我也尝试了自定义验证规则。 But it's working only if i will pull required rule alongwith.但它只有在我将要求的规则一起拉出来时才有效。

For example:例如:

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

You can use conditional rules for 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

Laravel evaluates each rule in the giver order. Laravel 会按照给出者的顺序评估每个规则。 Let's say:让我们说:

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

custom_b rule will be evaluate when required and custom_b are true because these rules were already evaluated. custom_b规则将在required时进行评估,并且custom_b为真,因为这些规则已经过评估。

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" and phone_input_active is true.如果目标等于“lead-capture”并且phone_input_active 为真,这将使“phone_input_label”成为必需。

The only problem with that custom validator is that you need to 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”,它甚至不会通过自定义验证器,不确定为什么会发生。

According to documentation you can make a complex required_if statement by using requiredIf mothod of validation Rule.根据文档,您可以通过使用requiredIf验证规则方法来制作复杂的required_if语句。

If you would like to construct a more complex condition for the required_if rule, you may use the Rule::requiredIf method.如果你想为 required_if 规则构造一个更复杂的条件,你可以使用 Rule::requiredIf 方法。 This method accepts a boolean or a closure.此方法接受布尔值或闭包。 When passed a closure, the closure should return true or false to indicate if the field under validation is required:当传递闭包时,闭包应返回 true 或 false 以指示是否需要验证字段:

use Illuminate\Validation\Rule;

Validator::make($request->all(), [
    'number_users' => Rule::requiredIf(function () {
        return $this->input('input_a') == 1 && $this->input('input_b') == 2;
    }),
]);

这是你的答案

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

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 验证文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM