Skip to content

Commit f901935

Browse files
committed
Added docs for NotIdenticalTo validator
1 parent 372e800 commit f901935

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

reference/constraints.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Validation Constraints Reference
2424
constraints/EqualTo
2525
constraints/NotEqualTo
2626
constraints/IdenticalTo
27+
constraints/NotIdenticalTo
2728

2829
constraints/Date
2930
constraints/DateTime
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
NotIdenticalTo
2+
===========
3+
4+
.. versionadded:: 2.3
5+
This constraint is new in version 2.3.
6+
7+
Validates that a value is **not** identical to another value, defined in the
8+
options. To force that a value is identical, see
9+
:doc:`/reference/constraints/IdenticalTo`.
10+
11+
.. caution::
12+
13+
This constraint compares using ``!==``, so ``3`` and ``"3"`` are
14+
considered not equal. Use :doc:`/reference/constraints/NotEqualTo` 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\\NotIdenticalTo` |
24+
+----------------+-----------------------------------------------------------------------------+
25+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotIdenticalToValidator`|
26+
+----------------+-----------------------------------------------------------------------------+
27+
28+
Basic Usage
29+
-----------
30+
31+
If you want to ensure that the ``age`` of a ``Person`` class is *not* equal to
32+
``15`` and *not* 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+
- NotIdenticalTo:
43+
value: 15
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\NotIdenticalTo(
56+
* value = 15
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="NotIdenticalTo">
68+
<option name="value">15</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\NotIdenticalTo(array(
86+
'value' => 15,
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 not 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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Comparison Constraints
3232
* :doc:`EqualTo </reference/constraints/EqualTo>`
3333
* :doc:`NotEqualTo </reference/constraints/NotEqualTo>`
3434
* :doc:`IdenticalTo </reference/constraints/IdenticalTo>`
35+
* :doc:`NotIdenticalTo </reference/constraints/NotIdenticalTo>`
3536

3637
Date Constraints
3738
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)