简体   繁体   中英

How to combine another Laravel validation rule with 'required_without_all'

I am working with Laravel 5.3.

I am trying to implement the 'required_without_all' validation rule. I have it working as expected, however I want to add an additional rule (ie a minimum value rule). But I want this additional validation rule to run only if there is a value present in the field.

So to summarise, I have several fields and want the following validation:

  • At least one field out of them should be filled in (achieved this using the 'required_without_rule').
  • For fields that are filled in, I want a minimum value rule.

How can I achieve this?

First validation 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.

Second validation rule: 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

Third validation rule: 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.


So the validation rules for your fields should look like:

'my_field' => 'required_without_all:field,another_field | sometimes | min:40'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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