Skip to content

Commit 8a026ea

Browse files
committed
Merge pull request symfony#2599 from WouterJ/document_equal_validators
Documented the Comparison validators
2 parents 1d50d04 + 6f261f4 commit 8a026ea

11 files changed

+813
-0
lines changed

reference/constraints.rst

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ Validation Constraints Reference
2121

2222
constraints/Range
2323

24+
constraints/EqualTo
25+
constraints/NotEqualTo
26+
constraints/IdenticalTo
27+
constraints/NotIdenticalTo
28+
constraints/LessThan
29+
constraints/LessThanOrEqual
30+
constraints/GreaterThan
31+
constraints/GreaterThanOrEqual
32+
2433
constraints/Date
2534
constraints/DateTime
2635
constraints/Time

reference/constraints/EqualTo.rst

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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/NotEqualTo`.
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+
.. include:: /reference/constraints/_comparison-value-option.rst.inc
94+
95+
message
96+
~~~~~~~
97+
98+
**type**: ``string`` **default**: ``This value should be equal to {{ compared_value }}``
99+
100+
This is the message that will be shown if the value is not equal.

reference/constraints/GreaterThan.rst

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

0 commit comments

Comments
 (0)