Skip to content

Commit 966f3c1

Browse files
committed
[reference][validation] Adding a quick doc for the UniqueEntity constraint
1 parent c12fa7e commit 966f3c1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

reference/constraints.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Validation Constraints Reference
2121
constraints/MinLength
2222
constraints/NotBlank
2323
constraints/NotNull
24+
constraints/UniqueEntity
2425
constraints/Regex
2526
constraints/Time
2627
constraints/Url
@@ -52,6 +53,7 @@ The following constraints are natively available in Symfony2:
5253
* :doc:`MinLength <constraints/MinLength>`
5354
* :doc:`NotBlank <constraints/NotBlank>`
5455
* :doc:`NotNull <constraints/NotNull>`
56+
* :doc:`UniqueEntity <constraints/UniqueEntity>`
5557
* :doc:`Regex <constraints/Regex>`
5658
* :doc:`Time <constraints/Time>`
5759
* :doc:`Url <constraints/Url>`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
UniqueEntity
2+
============
3+
4+
Validates that a particular field (or fields) in a Doctrine entity are unique.
5+
For example, suppose you have an ``AcmeUserBundle`` with a ``User`` entity
6+
that has an ``email`` field. You can use the ``Unique`` constraint to guarantee
7+
that the ``email`` field remains unique between all of the constrains in your
8+
user table:
9+
10+
.. configuration-block::
11+
12+
.. code-block:: php-annotations
13+
14+
// Acme/UserBundle/Entity/User.php
15+
use Symfony\Component\Validator\Constraints as Assert;
16+
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* @ORM\Entity
21+
* @DoctrineAssert\UniqueEntity("email")
22+
*/
23+
class Author
24+
{
25+
/**
26+
* @var string $email
27+
*
28+
* @ORM\Column(name="email", type="string", length=255, unique=true)
29+
* @Assert\Email()
30+
*/
31+
protected $email;
32+
33+
// ...
34+
}
35+
36+
.. code-block:: yaml
37+
38+
# src/Acme/UserBundle/Resources/config/validation.yml
39+
constraints:
40+
- UniqueEntity: email
41+
42+
Options
43+
-------
44+
45+
* ``fields``: The field (or list of fields) on which this entity should be
46+
unique. For example,

0 commit comments

Comments
 (0)