Skip to content

Commit 90d54b3

Browse files
committed
Some corrections of errorPath documentation for UniqueEntity constraint
1 parent 32c1985 commit 90d54b3

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

reference/constraints/UniqueEntity.rst

+48-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using an email address that already exists in the system.
1212
| | - `message`_ |
1313
| | - `em`_ |
1414
| | - `repositoryMethod`_ |
15-
| | - `errorPath`_ |
15+
| | - `errorPath`_ |
1616
| | - `ignoreNull`_ |
1717
+----------------+-------------------------------------------------------------------------------------+
1818
| Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\UniqueEntity` |
@@ -155,7 +155,7 @@ method should return a countable result.
155155
errorPath
156156
~~~~~~~~~
157157

158-
**type**: ``string`` **default**: ``The name of the first`` `field`_
158+
**type**: ``string`` **default**: The name of the first `field`_
159159

160160
.. versionadded:: 2.1
161161
The ``errorPath`` option was added in Symfony 2.1.
@@ -180,6 +180,7 @@ Consider this example:
180180
181181
.. code-block:: php-annotations
182182
183+
// src/Acme/AdministrationBundle/Entity/Service.php
183184
namespace Acme\AdministrationBundle\Entity;
184185
185186
use Doctrine\ORM\Mapping as ORM;
@@ -206,7 +207,51 @@ Consider this example:
206207
public $port;
207208
}
208209
209-
Now, the message would be bound to the form field of the `port` with this configuration.
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 form field of the ``port`` with this configuration.
210255

211256

212257
ignoreNull

0 commit comments

Comments
 (0)