4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class Collection extends Composite
24 const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
25 const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
27 protected static $errorNames = array(
28 self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
29 self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
32 public $fields = array();
33 public $allowExtraFields = false;
34 public $allowMissingFields = false;
35 public $extraFieldsMessage = 'This field was not expected.';
36 public $missingFieldsMessage = 'This field is missing.';
41 public function __construct($options = null)
43 // no known options set? $options is the fields array
44 if (is_array($options)
45 && !array_intersect(array_keys($options), array('groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'))) {
46 $options = array('fields' => $options);
49 parent::__construct($options);
55 protected function initializeNestedConstraints()
57 parent::initializeNestedConstraints();
59 if (!is_array($this->fields)) {
60 throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__));
63 foreach ($this->fields as $fieldName => $field) {
64 // the XmlFileLoader and YamlFileLoader pass the field Optional
65 // and Required constraint as an array with exactly one element
66 if (is_array($field) && count($field) == 1) {
67 $this->fields[$fieldName] = $field = $field[0];
70 if (!$field instanceof Optional && !$field instanceof Required) {
71 $this->fields[$fieldName] = $field = new Required($field);
76 public function getRequiredOptions()
78 return array('fields');
81 protected function getCompositeOption()