1

My variable carType gets a string value.

If additionalFields variable is carType should be required when it is a truck or caravan.

If carType is a truck, then additionalFields should be array.

'carType' => 'string',
'additionalFields' => 'required_if:carType,truck,caravan',

How do I add an array validation based on condition?

greeflas
  • 825
  • 1
  • 8
  • 20
John
  • 11
  • 1
  • https://www.manifest.uk.com/blog/laravel-conditional-validation-rules – RiggsFolly Nov 10 '21 at 07:09
  • You can accept both 'array|object' as validation rule and then check in your logic whether the given data is an array or object. If you want to do this in your validation, then you can check the value of `$this->get('carType')` if it's a caravan or truck and array_merge the rules in your other rules. – Dennis Nov 10 '21 at 07:20
  • Hope that this is what you want https://stackoverflow.com/q/42320292/9892571 – Rex Nov 11 '21 at 03:25

0 Answers0