Skip to content
Advertisement

Validating non-required associative array with required fields

I want to validate that if my request contains a field of associative array type, that array needs to contain specific fields (but only if it’s present)

Ie, array, if present, needs to contain foo:

JavaScript

and

JavaScript

is fine, but

JavaScript

and

JavaScript

is not.

I’ve written this validator:

JavaScript

But it doesn’t accept the first scenario.

JavaScript

I’d like to avoid using 'data.array.foo' => 'required_with:data.array' (it also doesn’t work for scenario when array is empty)

JavaScript

Are there any non-custom validators that will help me accomplish this?

Advertisement

Answer

You can also use Rule::requiredIf after Laravel 5.6 and up, try this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement