1
votes

What's the Symfony Validator way of dealing with the case when a key in an array must exist but the value can be empty? This doc says about the Optional constraint, but this constraint allows the key to be missing in the array.

Say my code expects the following structure:

['foo'=>'bar', 'baz'=>'']

How do I get the Validator to complain when the baz key is missing? It can be empty though. Also is where a way to make sure it follows a constraint if it's not empty?

For example Laravel has the present constraint. Do I really have to code array_key_exists manually? Must be a very common case.

Also is it possible to validate a collection by the prototype of its element, just like the Configuration component can do?

1
have you tried the allowMissingFields option?Juan I. Morales Pestana

1 Answers

0
votes

You could follow this doc and implement a callback validation assert and do an array_key_exist or follow the same logic and add the allowMissingFields option.