Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c6b713

Browse filesBrowse files
committed
Merge pull request #2797 from naitsirch/error_path
Added documentation for errorPath option of UniqueEntity constraint
2 parents 667768c + 90d54b3 commit 3c6b713
Copy full SHA for 3c6b713

File tree

Expand file treeCollapse file tree

1 file changed

+107
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+107
-1
lines changed
Open diff view settings
Collapse file

‎reference/constraints/UniqueEntity.rst‎

Copy file name to clipboardExpand all lines: reference/constraints/UniqueEntity.rst
+107-1Lines changed: 107 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
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,120 @@ 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+
// 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 form field of the ``port`` with this configuration.
255+
156256

157257
ignoreNull
158258
~~~~~~~~~~
159259

160260
**type**: ``Boolean`` **default**: ``true``
161261

262+
.. versionadded:: 2.1
263+
The ``ignoreNull`` option was added in Symfony 2.1.
264+
162265
If this option is set to ``true``, then the constraint will allow multiple
163266
entities to have a ``null`` value for a field without failing validation.
164267
If set to ``false``, only one ``null`` value is allowed - if a second entity
165268
also has a ``null`` value, validation would fail.
269+
270+
271+
.. _`field`: `fields`_

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.