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