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 d2f1536

Browse filesBrowse files
committed
feature #16713 [Validator] Add the fields option to the Unique constraint (wkania)
This PR was merged into the 6.1 branch. Discussion ---------- [Validator] Add the fields option to the Unique constraint Maybe I will add an example of use. Commits ------- dcbad67 Added basic example caf3e1f [Validator] Add the fields option to the Unique constraint
2 parents f5785b2 + dcbad67 commit d2f1536
Copy full SHA for d2f1536

File tree

1 file changed

+94
-0
lines changed
Filter options

1 file changed

+94
-0
lines changed

‎reference/constraints/Unique.rst

Copy file name to clipboardExpand all lines: reference/constraints/Unique.rst
+94Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,100 @@ strings:
103103
Options
104104
-------
105105

106+
``fields``
107+
~~~~~~~~~~
108+
109+
**type**: ``array`` | ``string``
110+
111+
112+
.. versionadded:: 6.1
113+
114+
The ``fields`` option was introduced in Symfony 6.1.
115+
116+
This is defines the key or keys in a collection that should be checked for
117+
uniqueness. By default, all collection keys are checked for uniqueness.
118+
119+
For instance, assume you have a collection of items that contain a
120+
``latitude``, ``longitude`` and ``label`` fields. By default, you can have
121+
duplicate coordinates as long as the label is different. By setting the
122+
``fields`` option, you can force latitude+longitude to be unique in the
123+
collection::
124+
125+
.. configuration-block::
126+
127+
.. code-block:: php-annotations
128+
129+
// src/Entity/Poi.php
130+
namespace App\Entity;
131+
132+
use Symfony\Component\Validator\Constraints as Assert;
133+
134+
class Poi
135+
{
136+
/**
137+
* @Assert\Unique(fields={"latitude", "longitude"})
138+
*/
139+
protected $coordinates;
140+
}
141+
142+
.. code-block:: php-attributes
143+
144+
// src/Entity/Poi.php
145+
namespace App\Entity;
146+
147+
use Symfony\Component\Validator\Constraints as Assert;
148+
149+
class Poi
150+
{
151+
#[Assert\Unique(fields=['latitude', 'longitude'])]
152+
protected $coordinates;
153+
}
154+
155+
.. code-block:: yaml
156+
157+
# config/validator/validation.yaml
158+
App\Entity\Poi:
159+
properties:
160+
contactEmails:
161+
- Unique:
162+
fields: [latitude, longitude]
163+
164+
.. code-block:: xml
165+
166+
<!-- config/validator/validation.xml -->
167+
<?xml version="1.0" encoding="UTF-8" ?>
168+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
169+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
170+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
171+
172+
<class name="App\Entity\Poi">
173+
<property name="coordinates">
174+
<constraint name="Unique">
175+
<field>latitude</field>
176+
<field>longitude</field>
177+
</constraint>
178+
</property>
179+
</class>
180+
</constraint-mapping>
181+
182+
.. code-block:: php
183+
184+
// src/Entity/Poi.php
185+
namespace App\Entity;
186+
187+
use Symfony\Component\Validator\Constraints as Assert;
188+
use Symfony\Component\Validator\Mapping\ClassMetadata;
189+
190+
class Poi
191+
{
192+
public static function loadValidatorMetadata(ClassMetadata $metadata)
193+
{
194+
$metadata->addPropertyConstraint('contactEmails', new Assert\Unique([
195+
'fields' => ['latitude', 'longitude'],
196+
]));
197+
}
198+
}
199+
106200
.. include:: /reference/constraints/_groups-option.rst.inc
107201

108202
``message``

0 commit comments

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