Skip to content

Commit 372e800

Browse files
committed
Added docs for IdenticalTo validator
1 parent 48ca4f0 commit 372e800

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Validation Constraints Reference
2323

2424
constraints/EqualTo
2525
constraints/NotEqualTo
26+
constraints/IdenticalTo
2627

2728
constraints/Date
2829
constraints/DateTime

reference/constraints/IdenticalTo.rst

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

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Comparison Constraints
3131

3232
* :doc:`EqualTo </reference/constraints/EqualTo>`
3333
* :doc:`NotEqualTo </reference/constraints/NotEqualTo>`
34+
* :doc:`IdenticalTo </reference/constraints/IdenticalTo>`
3435

3536
Date Constraints
3637
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)