验证规则 required_if 与其他条件(Laravel 5.4)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42320292/
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 15:21:19  来源:igfitidea点击:

Validation rules required_if with other condition (Laravel 5.4)

phplaravelvalidationlaravel-5.4validationrules

提问by kesm0

I got a problem with validation rules with nested conditions.

我遇到了带有嵌套条件的验证规则的问题。

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer',
                    'external_id' => 'required_if:type_id,==,3|integer',
                ];
        }
}

Indeed I want to : - check the external_id only if the type_id equal to 3 - and check if it's an integer.

事实上,我想: - 仅当 type_id 等于 3 时才检查 external_id - 并检查它是否是一个整数。

When I post my form, the rules works when I select a type_id equal to 3. But if I select another type_id, like 1 or 2, the validation does not pass :

当我发布表单时,规则在我选择一个等于 3 的 type_id 时起作用。但是如果我选择另一个 type_id,例如 1 或 2,则验证不会通过:

The external_id must be an integer.

external_id 必须是整数。

I try to add the nullable condition but required_if does not work anymore

我尝试添加可为空的条件,但 required_if 不再起作用

Did you have any idea ?

你有什么想法吗?

采纳答案by Graham

Your rule performs two checks that are independent of one another; just because the external_idfield is not required when the type_id!= 3, does not mean the integer check is ignored.

您的规则执行两项相互独立的检查;仅仅因为external_idtype_id!= 3时不需要该字段,并不意味着忽略整数检查。

What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :

您正在寻找的是条件规则,它使您可以更好地控制何时执行检查,例如:

$validator = Validator::make($data, [
    'type_id'   => 'required|integer'
]);

$validator->sometimes('external_id', 'required|integer', function($input) {
    return $input->type_id == 3;
});

When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance()method:

使用表单验证时,您可以通过覆盖该getValidatorInstance()方法来访问底层验证器实例:

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer'
                ];
        }

        protected function getValidatorInstance() {
            $validator = parent::getValidatorInstance();
            $validator->sometimes('external_id', 'required|integer', function($input) {
                return $input->type_id == 3;
            });
            return $validator;
        }
}

回答by Nick A

Just came across the same problem and found the following answer that seems to work for me:

刚刚遇到了同样的问题,并找到了以下似乎对我有用的答案:

issue-using-required-if-validation-rule-in-form-builder

问题-使用-要求-如果-验证-规则-形式-构建器

     return [
                'type_id'     => 'required|integer',
                'external_id' => 'required_if:type_id,==,3|nullable|integer',
            ];

Result for me:

我的结果:

field not populated, type id not 3 - pass

字段未填充,类型 id 不是 3 - 通过

field not populated, type id 3 - fail - required field

字段未填充,类型 id 3 - 失败 - 必填字段

field populated, type id 3, non-integer - fail in integer rule

字段已填充,类型 id 3,非整数 - 整数规则失败

field populated, type id 3, integer - pass - all good!

字段已填充,类型 id 3,整数 - 通过 - 一切顺利!

note - think nullable rule came in Laravel 5.3

注意 - 认为可空规则来自 Laravel 5.3

回答by ashanrupasinghe

try this,

尝试这个,

  class StoreRequest extends Request
    {
            public function authorize(){
            return true;
            }

            public function rules(){
                    return [
                        'type_id'     => 'required|integer',
                        'external_id' => 'required_if:type_id|in:3|integer',
                    ];
            }
    }