Symfony validator conditional validation

151 views Asked by At
 * @Assert\Type(type="array")
 * @Assert\Count(
 *     min=5,
 *     max=10
 * )

I need validate a type array and then I need to validate if the array has a min and max count.

But when I send a string value, then Assert\Type evaluates that's not a valid type, but despite that validation continues to Assert\Count and there is problem it is not an array and Assert\Count throw UnexpectedValueException and I have message: This value should be of type {{ type }}., and the that's not the correct validation message for me.

Is possible do Assert\Count only when is Assert\Type condition correct?

1

There are 1 answers

0
Bossman On BEST ANSWER

If i am understanding your question then using Assert Sequentially is the resolution.

Update your Asserts to look like this:

 * @Assert\Sequentially({
 *     @Assert\Type(type="array"),
 *     @Assert\Count(min=5, max=10),
 * })
 */

The first validation will be raised if your Type is not an array and not perform any further validations.