@@ -12,6 +12,7 @@ using an email address that already exists in the system.
12
12
| | - `message `_ |
13
13
| | - `em `_ |
14
14
| | - `repositoryMethod `_ |
15
+ | | - `errorPath `_ |
15
16
| | - `ignoreNull `_ |
16
17
+----------------+-------------------------------------------------------------------------------------+
17
18
| Class | :class: `Symfony\\ Bridge\\ Doctrine\\ Validator\\ Constraints\\ UniqueEntity ` |
@@ -151,14 +152,116 @@ The name of the repository method to use for making the query to determine the
151
152
uniqueness. If it's left blank, the ``findBy `` method will be used. This
152
153
method should return a countable result.
153
154
155
+ errorPath
156
+ ~~~~~~~~~
157
+
158
+ **type **: ``string `` **default **: The name of the first `field `_
159
+
154
160
.. versionadded :: 2.1
155
- The ``ignoreNull `` option was added in Symfony 2.1.
161
+ The ``errorPath `` option was added in Symfony 2.1.
162
+
163
+ If the entity violates constraint the error message is bound to the first
164
+ field in `fields `_. If there are more than one fields, you may want to map
165
+ the error message to another field.
166
+
167
+ Consider this example:
168
+
169
+ .. configuration-block ::
170
+
171
+ .. code-block :: yaml
172
+
173
+ # src/Acme/AdministrationBundle/Resources/config/validation.yml
174
+ Acme\AdministrationBundle\Entity\Service :
175
+ constraints :
176
+ - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity :
177
+ fields : [host, port]
178
+ errorPath : port
179
+ message : ' This port is already in use on that host.'
180
+
181
+ .. code-block :: php-annotations
182
+
183
+ // src/Acme/AdministrationBundle/Entity/Service.php
184
+ namespace Acme\AdministrationBundle\Entity;
185
+
186
+ use Doctrine\ORM\Mapping as ORM;
187
+ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
188
+
189
+ /**
190
+ * @ORM\Entity
191
+ * @UniqueEntity(
192
+ * fields={"host", "port"},
193
+ * errorPath="port",
194
+ * message="This port is already in use on that host."
195
+ * )
196
+ */
197
+ class Service
198
+ {
199
+ /**
200
+ * @ORM\ManyToOne(targetEntity="Host")
201
+ */
202
+ public $host;
203
+
204
+ /**
205
+ * @ORM\Column(type="integer")
206
+ */
207
+ public $port;
208
+ }
209
+
210
+ .. code-block :: xml
211
+
212
+ <!-- src/Acme/AdministrationBundle/Resources/config/validation.xml -->
213
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
214
+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
215
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
216
+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
217
+
218
+ <class name =" Acme\AdministrationBundle\Entity\Service" >
219
+ <constraint name =" Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity" >
220
+ <option name =" field" >
221
+ <value >host</value >
222
+ <value >port</value >
223
+ </option >
224
+ <option name =" errorPath" >port</option >
225
+ <option name =" message" >This port is already in use on that host.</option >
226
+ </constraint >
227
+ </class >
228
+
229
+ </constraint-mapping >
230
+
231
+ .. code-block :: php
232
+
233
+ // src/Acme/AdministrationBundle/Entity/Service.php
234
+ namespace Acme\AdministrationBundle\Entity;
235
+
236
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
237
+ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
238
+
239
+ class Service
240
+ {
241
+ public $host;
242
+ public $port;
243
+
244
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
245
+ {
246
+ $metadata->addConstraint(new UniqueEntity(array(
247
+ 'fields' => array('host', 'port'),
248
+ 'errorPath' => 'port',
249
+ 'message' => 'This port is already in use on that host.',
250
+ )));
251
+ }
252
+ }
253
+
254
+ Now, the message would be bound to the ``port `` field with this configuration.
255
+
156
256
157
257
ignoreNull
158
258
~~~~~~~~~~
159
259
160
260
**type **: ``Boolean `` **default **: ``true ``
161
261
262
+ .. versionadded :: 2.1
263
+ The ``ignoreNull `` option was added in Symfony 2.1.
264
+
162
265
If this option is set to ``true ``, then the constraint will allow multiple
163
266
entities to have a ``null `` value for a field without failing validation.
164
267
If set to ``false ``, only one ``null `` value is allowed - if a second entity
0 commit comments