|
| 1 | +Unique |
| 2 | +====== |
| 3 | + |
| 4 | +Validates that all the elements of the given collection are unique (none of them |
| 5 | +is present more than once). Elements are compared strictly, so ``'7'`` and ``7`` |
| 6 | +are considered different elements (a string and an integer, respectively). |
| 7 | + |
| 8 | +.. seealso:: |
| 9 | + |
| 10 | + If you want to apply different validation constraints to the elements of a |
| 11 | + collection or want to make sure that certain collection keys are present, |
| 12 | + use the :doc:`Collection constraint </reference/constraints/Collection>`. |
| 13 | + |
| 14 | +.. seealso:: |
| 15 | + |
| 16 | + If you want to validate that the value of an entity property is unique among |
| 17 | + all entities of the same type (e.g. the registration email of all users) use |
| 18 | + the :doc:`UniqueEntity constraint </reference/constraints/UniqueEntity>`. |
| 19 | + |
| 20 | +========== =================================================================== |
| 21 | +Applies to :ref:`property or method <validation-property-target>` |
| 22 | +Options - `groups`_ |
| 23 | + - `message`_ |
| 24 | + - `payload`_ |
| 25 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\Unique` |
| 26 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\UniqueValidator` |
| 27 | +========== =================================================================== |
| 28 | + |
| 29 | +Basic Usage |
| 30 | +----------- |
| 31 | + |
| 32 | +This constraint can be applied to any property of type ``array`` or |
| 33 | +``\Traversable``. In the following example, ``$contactEmails`` is an array of |
| 34 | +strings: |
| 35 | + |
| 36 | +.. configuration-block:: |
| 37 | + |
| 38 | + .. code-block:: php-annotations |
| 39 | +
|
| 40 | + // src/Entity/Person.php |
| 41 | + namespace App\Entity; |
| 42 | +
|
| 43 | + use Symfony\Component\Validator\Constraints as Assert; |
| 44 | +
|
| 45 | + class Person |
| 46 | + { |
| 47 | + /** |
| 48 | + * @Assert\Unique |
| 49 | + */ |
| 50 | + protected $contactEmails; |
| 51 | + } |
| 52 | +
|
| 53 | + .. code-block:: yaml |
| 54 | +
|
| 55 | + # config/validator/validation.yaml |
| 56 | + App\Entity\Person: |
| 57 | + properties: |
| 58 | + contactEmails: |
| 59 | + - Unique: ~ |
| 60 | +
|
| 61 | + .. code-block:: xml |
| 62 | +
|
| 63 | + <!-- config/validator/validation.xml --> |
| 64 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 65 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 66 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 67 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 68 | +
|
| 69 | + <class name="App\Entity\Person"> |
| 70 | + <property name="contactEmails"> |
| 71 | + <constraint name="Unique"/> |
| 72 | + </property> |
| 73 | + </class> |
| 74 | + </constraint-mapping> |
| 75 | +
|
| 76 | + .. code-block:: php |
| 77 | +
|
| 78 | + // src/Entity/Person.php |
| 79 | + namespace App\Entity; |
| 80 | +
|
| 81 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 82 | + use Symfony\Component\Validator\Constraints as Assert; |
| 83 | +
|
| 84 | + class Person |
| 85 | + { |
| 86 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 87 | + { |
| 88 | + $metadata->addPropertyConstraint('contactEmails', new Assert\Unique()); |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | +Options |
| 93 | +------- |
| 94 | + |
| 95 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 96 | + |
| 97 | +message |
| 98 | +~~~~~~~ |
| 99 | + |
| 100 | +**type**: ``string`` **default**: ``This collection should contain only unique elements.`` |
| 101 | + |
| 102 | +This is the message that will be shown if at least one element is repeated in |
| 103 | +the collection. |
| 104 | + |
| 105 | +You can use the following parameters in this message: |
| 106 | + |
| 107 | +============================= ================================================ |
| 108 | +Parameter Description |
| 109 | +============================= ================================================ |
| 110 | +``{{ value }}`` The repeated value |
| 111 | +============================= ================================================ |
| 112 | + |
| 113 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments