@@ -103,6 +103,100 @@ strings:
103
103
Options
104
104
-------
105
105
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
8000
td>+ 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
+
106
200
.. include :: /reference/constraints/_groups-option.rst.inc
107
201
108
202
``message ``
0 commit comments