如何将另一个 Laravel 验证规则与 'required_without_all' 相结合
How to combine another Laravel validation rule with 'required_without_all'
我正在与 Laravel 5.3 合作。
我正在尝试实施 'required_without_all' 验证规则。我让它按预期工作,但是我想添加一个额外的规则(即最小值规则)。但是我希望这个额外的验证规则 运行 只有 如果字段中存在值。
总而言之,我有几个字段并需要以下验证:
- 至少要填写其中的一个字段(实现了这个
使用 'required_without_rule').
- 对于填写的字段,我
想要一个最小值规则。
我怎样才能做到这一点?
第一个验证规则:required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are not present.
第二个验证规则:sometimes
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list
第三个验证规则:min:value
The field under validation must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
因此您的字段的验证规则应如下所示:
'my_field' => 'required_without_all:field,another_field | sometimes | min:40'
我正在与 Laravel 5.3 合作。
我正在尝试实施 'required_without_all' 验证规则。我让它按预期工作,但是我想添加一个额外的规则(即最小值规则)。但是我希望这个额外的验证规则 运行 只有 如果字段中存在值。
总而言之,我有几个字段并需要以下验证:
- 至少要填写其中的一个字段(实现了这个 使用 'required_without_rule').
- 对于填写的字段,我 想要一个最小值规则。
我怎样才能做到这一点?
第一个验证规则:required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are not present.
第二个验证规则:sometimes
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list
第三个验证规则:min:value
The field under validation must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
因此您的字段的验证规则应如下所示:
'my_field' => 'required_without_all:field,another_field | sometimes | min:40'