Skip to content

Commit 48ca4f0

Browse files
committed
Added docs for NotEqualTo validator
1 parent 635c8c8 commit 48ca4f0

File tree

5 files changed

+112
-8
lines changed

5 files changed

+112
-8
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Validation Constraints Reference
2222
constraints/Range
2323

2424
constraints/EqualTo
25+
constraints/NotEqualTo
2526

2627
constraints/Date
2728
constraints/DateTime

reference/constraints/EqualTo.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ EqualTo
55
This constraint is new in version 2.3.
66

77
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`.
8+
force that a value is *not* equal, see :doc:`/reference/constraints/NotEqualTo`.
99

1010
.. caution::
1111

@@ -90,13 +90,7 @@ If you want to ensure that the ``age`` of a ``Person`` class is equal to
9090
Options
9191
-------
9292

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.
93+
.. include:: /reference/constraints/_comparison-value-option.rst.inc
10094

10195
message
10296
~~~~~~~

reference/constraints/NotEqualTo.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
NotEqualTo
2+
==========
3+
4+
.. versionadded:: 2.3
5+
This constraint is new in version 2.3.
6+
7+
Validates that a value is **not** equal to another value, defined in the
8+
options. To force that a value is equal, see
9+
:doc:`/reference/constraints/EqualTo`.
10+
11+
.. caution::
12+
13+
This constraint compares using ``!=``, so ``3`` and ``"3"`` are considered
14+
equal. Use :doc:`/reference/constraints/NotIdenticalTo` to compare with
15+
``!==``.
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\\NotEqualTo` |
24+
+----------------+-------------------------------------------------------------------------+
25+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotEqualToValidator`|
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``, 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+
- NotEqualTo:
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\NotEqualTo(
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="NotEqualTo">
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\NotEqualTo(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 equal to {{ compared_value }}``
100+
101+
This is the message that will be shown if the value is not equal.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
value
2+
~~~~~
3+
4+
**type**: ``mixed``
5+
6+
This option is required. It defines the value to compare to. It can be a
7+
string, number or object.

reference/constraints/map.rst.inc

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

3232
* :doc:`EqualTo </reference/constraints/EqualTo>`
33+
* :doc:`NotEqualTo </reference/constraints/NotEqualTo>`
3334

3435
Date Constraints
3536
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)