Skip to content

Commit 50d8e47

Browse files
committed
Merge branch '2.1' into 2.2
2 parents 4eeec12 + 6f8439b commit 50d8e47

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

book/http_fundamentals.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ by adding an entry for ``/contact`` to your routing configuration file:
448448
449449
.. note::
450450

451-
This example uses :doc:`YAML</components/yaml/format>` to define the routing
451+
This example uses :doc:`YAML</components/yaml/yaml_format>` to define the routing
452452
configuration. Routing configuration can also be written in other formats
453453
such as XML or PHP.
454454

cookbook/session/proxy_examples.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
single: Sessions, session proxy, proxy
33

44
Session Proxy Examples
5-
----------------------
5+
======================
66

77
The session proxy mechanism has a variety of uses and this example demonstrates
88
two common uses. Rather than injecting the session handler as normal, a handler

reference/constraints/UniqueEntity.rst

+104-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,14 +152,116 @@ 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 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+
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

0 commit comments

Comments
 (0)