Skip to content

Commit 635c8c8

Browse files
committed
Added documentation for the EqualConstraint
1 parent 40af9fa commit 635c8c8

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

reference/constraints.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Validation Constraints Reference
2121

2222
constraints/Range
2323

24+
constraints/EqualTo
25+
2426
constraints/Date
2527
constraints/DateTime
2628
constraints/Time

reference/constraints/EqualTo.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
EqualTo
2+
=======
3+
4+
.. versionadded:: 2.3
5+
This constraint is new in version 2.3.
6+
7+
Validates that a value is equal to another value, defined in the options. To
8+
force that a value is *not* equal, see :doc:`/reference/constraints/NotEqual`.
9+
10+
.. caution::
11+
12+
This constraint compares using ``==``, so ``3`` and ``"3"`` are considered
13+
equal. Use :doc:`/reference/constraints/IdenticalTo` to compare with
14+
``===``.
15+
16+
+----------------+-----------------------------------------------------------------------+
17+
| Applies to | :ref:`property or method<validation-property-target>` |
18+
+----------------+-----------------------------------------------------------------------+
19+
| Options | - `value`_ |
20+
| | - `message`_ |
21+
+----------------+-----------------------------------------------------------------------+
22+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\EqualTo` |
23+
+----------------+-----------------------------------------------------------------------+
24+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\EqualToValidator` |
25+
+----------------+-----------------------------------------------------------------------+
26+
27+
Basic Usage
28+
-----------
29+
30+
If you want to ensure that the ``age`` of a ``Person`` class is equal to
31+
``20``, you could do the following:
32+
33+
.. configuration-block::
34+
35+
.. code-block:: yaml
36+
37+
# src/SocialBundle/Resources/config/validation.yml
38+
Acme\SocialBundle\Entity\Person:
39+
properties:
40+
age:
41+
- EqualTo:
42+
value: 20
43+
44+
.. code-block:: php-annotations
45+
46+
// src/Acme/SocialBundle/Entity/Person.php
47+
namespace Acme\SocialBundle\Entity;
48+
49+
use Symfony\Component\Validator\Constraints as Assert;
50+
51+
class Person
52+
{
53+
/**
54+
* @Assert\EqualTo(
55+
* value = 20
56+
* )
57+
*/
58+
protected $age;
59+
}
60+
61+
.. code-block:: xml
62+
63+
<!-- src/Acme/SocialBundle/Resources/config/validation.xml -->
64+
<class name="Acme\SocialBundle\Entity\Person">
65+
<property name="age">
66+
<constraint name="EqualTo">
67+
<option name="value">20</option>
68+
</constraint>
69+
</property>
70+
</class>
71+
72+
.. code-block:: php
73+
74+
// src/Acme/SocialBundle/Entity/Person.php
75+
namespace Acme\SocialBundle\Entity;
76+
77+
use Symfony\Component\Validator\Mapping\ClassMetadata;
78+
use Symfony\Component\Validator\Constraints as Assert;
79+
80+
class Person
81+
{
82+
public static function loadValidatorMetadata(ClassMetadata $metadata)
83+
{
84+
$metadata->addPropertyConstraint('age', new Assert\EqualTo(array(
85+
'value' => 20,
86+
)));
87+
}
88+
}
89+
90+
Options
91+
-------
92+
93+
value
94+
~~~~~
95+
96+
**type**: ``mixed``
97+
98+
This option is required. It defines the value to compare to. It can be a
99+
string, number or object.
100+
101+
message
102+
~~~~~~~
103+
104+
**type**: ``string`` **default**: ``This value should be equal to {{ compared_value }}``
105+
106+
This is the message that will be shown if the value is not equal.

reference/constraints/map.rst.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Number Constraints
2626

2727
* :doc:`Range </reference/constraints/Range>`
2828

29+
Comparison Constraints
30+
~~~~~~~~~~~~~~~~~~~~~~
31+
32+
* :doc:`EqualTo </reference/constraints/EqualTo>`
33+
2934
Date Constraints
3035
~~~~~~~~~~~~~~~~
3136

0 commit comments

Comments
 (0)