Skip to content

Commit 2926b92

Browse files
committed
added documentation for errorPath option of UniqueEntity constraint
1 parent 459b1a1 commit 2926b92

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

reference/constraints/UniqueEntity.rst

+62-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using an email address that already exists in the system.
1212
| | - `message`_ |
1313
| | - `em`_ |
1414
| | - `repositoryMethod`_ |
15+
| | - `errorPath`_ |
1516
| | - `ignoreNull`_ |
1617
+----------------+-------------------------------------------------------------------------------------+
1718
| Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\UniqueEntity` |
@@ -151,15 +152,75 @@ The name of the repository method to use for making the query to determine the
151152
uniqueness. If it's left blank, the ``findBy`` method will be used. This
152153
method should return a countable result.
153154

155+
errorPath
156+
~~~~~~~~~
157+
158+
**type**: ``string`` **default**: ``The name of the first`` `field`_
159+
154160
.. 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 against this constraint the error message is bound to
164+
the first field. If there are more than one fields it may be desired to bind the
165+
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+
namespace Acme\AdministrationBundle\Entity;
184+
185+
use Doctrine\ORM\Mapping as ORM;
186+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
187+
188+
/**
189+
* @ORM\Entity
190+
* @UniqueEntity(
191+
* fields={"host", "port"},
192+
* errorPath="port",
193+
* message="This port is already in use on that host."
194+
* )
195+
*/
196+
class Service
197+
{
198+
/**
199+
* @ORM\ManyToOne(targetEntity="Host")
200+
*/
201+
public $host;
202+
203+
/**
204+
* @ORM\Column(type="integer")
205+
*/
206+
public $port;
207+
}
208+
209+
Now, the message would be bound to the form field of the `port` with this configuration.
210+
156211

157212
ignoreNull
158213
~~~~~~~~~~
159214

160215
**type**: ``Boolean`` **default**: ``true``
161216

217+
.. versionadded:: 2.1
218+
The ``ignoreNull`` option was added in Symfony 2.1.
219+
162220
If this option is set to ``true``, then the constraint will allow multiple
163221
entities to have a ``null`` value for a field without failing validation.
164222
If set to ``false``, only one ``null`` value is allowed - if a second entity
165223
also has a ``null`` value, validation would fail.
224+
225+
226+
.. _`field`: `fields`_

0 commit comments

Comments
 (0)