-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added documentation for errorPath option of UniqueEntity constraint #2797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2926b92
added documentation for errorPath option of UniqueEntity constraint
leonex-cs1 32c1985
added new line to errorPath documentation of UniqueEntity constraint
leonex-cs1 90d54b3
Some corrections of errorPath documentation for UniqueEntity constraint
leonex-cs1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,120 @@ 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 | ||
|
||
// src/Acme/AdministrationBundle/Entity/Service.php | ||
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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. XML<!-- src/Acme/AdministrationBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="Acme\AdministrationBundle\Entity\Service">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="field">
<value>host</value>
<value>port</value>
</option>
<option name="errorPath">port</option>
<option name="message">This port is already in use on that host.</option>
</constraint>
</class>
</constraint-mapping> 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.',
)));
}
} |
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/AdministrationBundle/Resources/config/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="Acme\AdministrationBundle\Entity\Service"> | ||
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity"> | ||
<option name="field"> | ||
<value>host</value> | ||
<value>port</value> | ||
</option> | ||
<option name="errorPath">port</option> | ||
<option name="message">This port is already in use on that host.</option> | ||
</constraint> | ||
</class> | ||
|
||
</constraint-mapping> | ||
|
||
.. 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 | ||
~~~~~~~~~~ | ||
|
||
**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`_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you test this? I doubt if it works |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add a filename comment here?
// src/Acme/AdministrationBundle/Entity/Service.php