Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
Documented the new Unique constraint#11247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsreference/constraints.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionsreference/constraints/Collection.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletionsreference/constraints/Unique.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| Unique | ||
| ====== | ||
| Validates that all the elements of the given collection are unique (none of them | ||
| is present more than once). Elements are compared strictly, so ``'7'`` and ``7`` | ||
| are considered different elements (a string and an integer, respectively). | ||
| .. seealso:: | ||
| If you want to apply different validation constraints to the elements of a | ||
| collection or want to make sure that certain collection keys are present, | ||
| use the :doc:`Collection constraint </reference/constraints/Collection>`. | ||
| .. seealso:: | ||
| If you want to validate that the value of an entity property is unique among | ||
| all entities of the same type (e.g. the registration email of all users) use | ||
| the :doc:`UniqueEntity constraint </reference/constraints/UniqueEntity>`. | ||
| ========== =================================================================== | ||
| Applies to :ref:`property or method <validation-property-target>` | ||
| Options - `groups`_ | ||
| - `message`_ | ||
| - `payload`_ | ||
| Class :class:`Symfony\\Component\\Validator\\Constraints\\Unique` | ||
| Validator :class:`Symfony\\Component\\Validator\\Constraints\\UniqueValidator` | ||
| ========== =================================================================== | ||
| Basic Usage | ||
| ----------- | ||
| This constraint can be applied to any property of type ``array`` or | ||
| ``\Traversable``. In the following example, ``$contactEmails`` is an array of | ||
| strings: | ||
| .. configuration-block:: | ||
| .. code-block:: php-annotations | ||
| // src/Entity/Person.php | ||
| namespace App\Entity; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
| class Person | ||
| { | ||
| /** | ||
| * @Assert\Unique | ||
| */ | ||
| protected $contactEmails; | ||
| } | ||
| .. code-block:: yaml | ||
| # config/validator/validation.yaml | ||
| App\Entity\Person: | ||
| properties: | ||
| contactEmails: | ||
| - Unique: ~ | ||
| .. code-block:: xml | ||
| <!-- config/validator/validation.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
| <class name="App\Entity\Person"> | ||
| <property name="contactEmails"> | ||
| <constraint name="Unique"/> | ||
| </property> | ||
| </class> | ||
| </constraint-mapping> | ||
| .. code-block:: php | ||
| // src/Entity/Person.php | ||
| namespace App\Entity; | ||
| use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
| class Person | ||
| { | ||
| public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
| { | ||
| $metadata->addPropertyConstraint('contactEmails', new Assert\Unique()); | ||
| } | ||
| } | ||
| Options | ||
| ------- | ||
| .. include:: /reference/constraints/_groups-option.rst.inc | ||
| message | ||
| ~~~~~~~ | ||
| **type**: ``string`` **default**: ``This collection should contain only unique elements.`` | ||
| This is the message that will be shown if at least one element is repeated in | ||
| the collection. | ||
| You can use the following parameters in this message: | ||
| ============================= ================================================ | ||
| Parameter Description | ||
| ============================= ================================================ | ||
| ``{{ value }}`` The repeated value | ||
| ============================= ================================================ | ||
| .. include:: /reference/constraints/_payload-option.rst.inc |
5 changes: 5 additions & 0 deletionsreference/constraints/UniqueEntity.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsreference/constraints/map.rst.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.