From 2926b92c98b73ffecf38258fbe205c8d8bf27dac Mon Sep 17 00:00:00 2001 From: Christian Stoller Date: Fri, 5 Jul 2013 12:15:57 +0200 Subject: [PATCH 1/3] added documentation for errorPath option of UniqueEntity constraint --- reference/constraints/UniqueEntity.rst | 63 +++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index 27c0204c7a8..b3d15d82922 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -12,6 +12,7 @@ using an email address that already exists in the system. | | - `message`_ | | | - `em`_ | | | - `repositoryMethod`_ | +| | - `errorPath`_ | | | - `ignoreNull`_ | +----------------+-------------------------------------------------------------------------------------+ | 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 uniqueness. If it's left blank, the ``findBy`` method will be used. This method should return a countable result. +errorPath +~~~~~~~~~ + +**type**: ``string`` **default**: ``The name of the first`` `field`_ + .. versionadded:: 2.1 - The ``ignoreNull`` option was added in Symfony 2.1. + The ``errorPath`` option was added in Symfony 2.1. + +If the entity violates against this constraint the error message is bound to +the first field. If there are more than one fields it may be desired to bind the +error message to another field. + +Consider this example: + +.. configuration-block:: + + .. code-block:: yaml + + # src/Acme/AdministrationBundle/Resources/config/validation.yml + Acme\AdministrationBundle\Entity\Service: + constraints: + - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: + fields: [ host, port ] + errorPath: port + message: 'This port is already in use on that host.' + + .. code-block:: php-annotations + + namespace Acme\AdministrationBundle\Entity; + + use Doctrine\ORM\Mapping as ORM; + use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; + + /** + * @ORM\Entity + * @UniqueEntity( + * fields={"host", "port"}, + * errorPath="port", + * message="This port is already in use on that host." + * ) + */ + class Service + { + /** + * @ORM\ManyToOne(targetEntity="Host") + */ + public $host; + + /** + * @ORM\Column(type="integer") + */ + public $port; + } + +Now, the message would be bound to the form field of the `port` with this configuration. + ignoreNull ~~~~~~~~~~ **type**: ``Boolean`` **default**: ``true`` +.. versionadded:: 2.1 + The ``ignoreNull`` option was added in Symfony 2.1. + If this option is set to ``true``, then the constraint will allow multiple entities to have a ``null`` value for a field without failing validation. If set to ``false``, only one ``null`` value is allowed - if a second entity also has a ``null`` value, validation would fail. + + +.. _`field`: `fields`_ \ No newline at end of file From 32c198583fd5f371b05647af0057beb2f7b0eedc Mon Sep 17 00:00:00 2001 From: Christian Stoller Date: Fri, 5 Jul 2013 12:17:50 +0200 Subject: [PATCH 2/3] added new line to errorPath documentation of UniqueEntity constraint --- reference/constraints/UniqueEntity.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index b3d15d82922..cc9fe1408e9 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -223,4 +223,4 @@ If set to ``false``, only one ``null`` value is allowed - if a second entity also has a ``null`` value, validation would fail. -.. _`field`: `fields`_ \ No newline at end of file +.. _`field`: `fields`_ From 90d54b397866822b7f37b967f521fb0e57346182 Mon Sep 17 00:00:00 2001 From: Christian Stoller Date: Fri, 5 Jul 2013 14:23:57 +0200 Subject: [PATCH 3/3] Some corrections of errorPath documentation for UniqueEntity constraint --- reference/constraints/UniqueEntity.rst | 51 ++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index cc9fe1408e9..2b0ac544495 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -12,7 +12,7 @@ using an email address that already exists in the system. | | - `message`_ | | | - `em`_ | | | - `repositoryMethod`_ | -| | - `errorPath`_ | +| | - `errorPath`_ | | | - `ignoreNull`_ | +----------------+-------------------------------------------------------------------------------------+ | Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\UniqueEntity` | @@ -155,7 +155,7 @@ method should return a countable result. errorPath ~~~~~~~~~ -**type**: ``string`` **default**: ``The name of the first`` `field`_ +**type**: ``string`` **default**: The name of the first `field`_ .. versionadded:: 2.1 The ``errorPath`` option was added in Symfony 2.1. @@ -180,6 +180,7 @@ Consider this example: .. code-block:: php-annotations + // src/Acme/AdministrationBundle/Entity/Service.php namespace Acme\AdministrationBundle\Entity; use Doctrine\ORM\Mapping as ORM; @@ -206,7 +207,51 @@ Consider this example: public $port; } -Now, the message would be bound to the form field of the `port` with this configuration. + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + // src/Acme/AdministrationBundle/Entity/Service.php + namespace Acme\AdministrationBundle\Entity; + + use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; + + class Service + { + public $host; + public $port; + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addConstraint(new UniqueEntity(array( + 'fields' => array('host', 'port'), + 'errorPath' => 'port', + 'message' => 'This port is already in use on that host.', + ))); + } + } + +Now, the message would be bound to the form field of the ``port`` with this configuration. ignoreNull