symfony/symfony-docs — Issue #999: [Validator] Document Collection constraint in combination with Optional/Required
Author: bschussek
Created: 2012-01-17 11:24:03+00:00
State: open

As of 2.1, there are two new constraints that can be used with `Collection`: `Optional` and `Required`. They give more flexibility over the existing options `allowExtraFields` and `allowMissingFields`.

Usage:

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Collection\Optional;
use Symfony\Component\Validator\Constraints\Collection\Required;

/**
* @Assert\Collection({
* foo = @Required(),
* bar = @Optional(),
* baz = @Required(@Assert\Min(3)),
* bam = @Optional({@Assert\NotNull, @Assert\MinLength(7)})
* )}
private $options = array(
'foo' => null,
'baz' => 10,
);