Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4880aee

Browse filesBrowse files
committed
minor #11247 Documented the new Unique constraint (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #11247). Discussion ---------- Documented the new Unique constraint Documents symfony/symfony#30706 Commits ------- 4b2007e Documented the new Unique constraint
2 parents be6c5af + 4b2007e commit 4880aee
Copy full SHA for 4880aee

File tree

5 files changed

+125
-0
lines changed
Filter options

5 files changed

+125
-0
lines changed

‎reference/constraints.rst

Copy file name to clipboardExpand all lines: reference/constraints.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Validation Constraints Reference
3131
constraints/GreaterThanOrEqual
3232
constraints/Range
3333
constraints/DivisibleBy
34+
constraints/Unique
3435

3536
constraints/Date
3637
constraints/DateTime

‎reference/constraints/Collection.rst

Copy file name to clipboardExpand all lines: reference/constraints/Collection.rst
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ constraint.
1111
This constraint can also make sure that certain collection keys are present
1212
and that extra keys are not present.
1313

14+
.. seealso::
15+
16+
If you want to validate that all the elements of the collection are unique
17+
use the :doc:`Unique constraint </reference/constraints/Unique>`.
18+
1419
========== ===================================================================
1520
Applies to :ref:`property or method <validation-property-target>`
1621
Options - `allowExtraFields`_

‎reference/constraints/Unique.rst

Copy file name to clipboard
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

‎reference/constraints/UniqueEntity.rst

Copy file name to clipboardExpand all lines: reference/constraints/UniqueEntity.rst
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Validates that a particular field (or fields) in a Doctrine entity is (are)
55
unique. This is commonly used, for example, to prevent a new user to register
66
using an email address that already exists in the system.
77

8+
.. seealso::
9+
10+
If you want to validate that all the elements of the collection are unique
11+
use the :doc:`Unique constraint </reference/constraints/Unique>`.
12+
813
========== ===================================================================
914
Applies to :ref:`class <validation-class-target>`
1015
Options - `em`_

‎reference/constraints/map.rst.inc

Copy file name to clipboardExpand all lines: reference/constraints/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Comparison Constraints
3737
* :doc:`GreaterThanOrEqual </reference/constraints/GreaterThanOrEqual>`
3838
* :doc:`Range </reference/constraints/Range>`
3939
* :doc:`DivisibleBy </reference/constraints/DivisibleBy>`
40+
* :doc:`Unique </reference/constraints/Unique>`
4041

4142
Date Constraints
4243
~~~~~~~~~~~~~~~~

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.