Skip to content

Commit 28cc201

Browse files
committed
feature #5677 replacing deprecated usage of True, False, Null validators in docs (Tim Stamp)
This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #5677). Discussion ---------- replacing deprecated usage of True, False, Null validators in docs related: #5665 Commits ------- 9a5ea56 Updating book examples to not use deprecated validation methods 627d74d Updated constraint reference docs for Null->IsNull, False->IsFalse, True->IsTrue a90ea7c Creating placeholder constraint rst docs for deprecated usage 639be4b Renaming constraint rst files to Is* to preserve edit history
2 parents b2a9a6c + 9a5ea56 commit 28cc201

File tree

9 files changed

+345
-331
lines changed

9 files changed

+345
-331
lines changed

book/validation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ this method must return ``true``:
625625
class Author
626626
{
627627
/**
628-
* @Assert\True(message = "The password cannot match your first name")
628+
* @Assert\IsTrue(message = "The password cannot match your first name")
629629
*/
630630
public function isPasswordLegal()
631631
{
@@ -670,7 +670,7 @@ this method must return ``true``:
670670
{
671671
public static function loadValidatorMetadata(ClassMetadata $metadata)
672672
{
673-
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
673+
$metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
674674
'message' => 'The password cannot match your first name',
675675
)));
676676
}
@@ -928,7 +928,7 @@ username and the password are different only if all other validation passes
928928
private $password;
929929
930930
/**
931-
* @Assert\True(message="The password cannot match your username", groups={"Strict"})
931+
* @Assert\IsTrue(message="The password cannot match your username", groups={"Strict"})
932932
*/
933933
public function isPasswordLegal()
934934
{
@@ -1002,7 +1002,7 @@ username and the password are different only if all other validation passes
10021002
$metadata->addPropertyConstraint('username', new Assert\NotBlank());
10031003
$metadata->addPropertyConstraint('password', new Assert\NotBlank());
10041004
1005-
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
1005+
$metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
10061006
'message' => 'The password cannot match your first name',
10071007
'groups' => array('Strict'),
10081008
)));

reference/constraints.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Validation Constraints Reference
88
constraints/NotBlank
99
constraints/Blank
1010
constraints/NotNull
11-
constraints/Null
12-
constraints/True
13-
constraints/False
11+
constraints/IsNull
12+
constraints/IsTrue
13+
constraints/IsFalse
1414
constraints/Type
1515

1616
constraints/Email

reference/constraints/Blank.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Blank
33

44
Validates that a value is blank, defined as equal to a blank string or equal
55
to ``null``. To force that a value strictly be equal to ``null``, see the
6-
:doc:`/reference/constraints/Null` constraint. To force that a value is
6+
:doc:`/reference/constraints/IsNull` constraint. To force that a value is
77
*not* blank, see :doc:`/reference/constraints/NotBlank`.
88

99
+----------------+---------------------------------------------------------------------+

reference/constraints/False.rst

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,9 @@
11
False
22
=====
33

4-
Validates that a value is ``false``. Specifically, this checks to see if
5-
the value is exactly ``false``, exactly the integer ``0``, or exactly the
6-
string "``0``".
7-
8-
Also see :doc:`True <True>`.
9-
10-
+----------------+---------------------------------------------------------------------+
11-
| Applies to | :ref:`property or method <validation-property-target>` |
12-
+----------------+---------------------------------------------------------------------+
13-
| Options | - `message`_ |
14-
+----------------+---------------------------------------------------------------------+
15-
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\False` |
16-
+----------------+---------------------------------------------------------------------+
17-
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\FalseValidator` |
18-
+----------------+---------------------------------------------------------------------+
19-
20-
Basic Usage
21-
-----------
22-
23-
The ``False`` constraint can be applied to a property or a "getter" method,
24-
but is most commonly useful in the latter case. For example, suppose that
25-
you want to guarantee that some ``state`` property is *not* in a dynamic
26-
``invalidStates`` array. First, you'd create a "getter" method::
27-
28-
protected $state;
29-
30-
protected $invalidStates = array();
31-
32-
public function isStateInvalid()
33-
{
34-
return in_array($this->state, $this->invalidStates);
35-
}
36-
37-
In this case, the underlying object is only valid if the ``isStateInvalid``
38-
method returns **false**:
39-
40-
.. configuration-block::
41-
42-
.. code-block:: php-annotations
43-
44-
// src/AppBundle/Entity/Author.php
45-
namespace AppBundle\Entity;
46-
47-
use Symfony\Component\Validator\Constraints as Assert;
48-
49-
class Author
50-
{
51-
/**
52-
* @Assert\False(
53-
* message = "You've entered an invalid state."
54-
* )
55-
*/
56-
public function isStateInvalid()
57-
{
58-
// ...
59-
}
60-
}
61-
62-
.. code-block:: yaml
63-
64-
# src/AppBundle/Resources/config/validation.yml
65-
AppBundle\Entity\Author
66-
getters:
67-
stateInvalid:
68-
- 'False':
69-
message: You've entered an invalid state.
70-
71-
.. code-block:: xml
72-
73-
<!-- src/AppBundle/Resources/config/validation.xml -->
74-
<?xml version="1.0" encoding="UTF-8" ?>
75-
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
76-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
77-
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
78-
79-
<class name="AppBundle\Entity\Author">
80-
<getter property="stateInvalid">
81-
<constraint name="False">
82-
<option name="message">You've entered an invalid state.</option>
83-
</constraint>
84-
</getter>
85-
</class>
86-
</constraint-mapping>
87-
88-
.. code-block:: php
89-
90-
// src/AppBundle/Entity/Author.php
91-
namespace AppBundle\Entity;
92-
93-
use Symfony\Component\Validator\Mapping\ClassMetadata;
94-
use Symfony\Component\Validator\Constraints as Assert;
95-
96-
class Author
97-
{
98-
public static function loadValidatorMetadata(ClassMetadata $metadata)
99-
{
100-
$metadata->addGetterConstraint('stateInvalid', new Assert\False());
101-
}
102-
}
103-
1044
.. caution::
1055

106-
When using YAML, be sure to surround ``False`` with quotes (``'False'``)
107-
or else YAML will convert this into a ``false`` boolean value.
108-
109-
Options
110-
-------
111-
112-
message
113-
~~~~~~~
114-
115-
**type**: ``string`` **default**: ``This value should be false.``
6+
The ``False`` constraint is deprecated since Symfony 2.7
7+
and will be removed in Symfony 3.0. Use the ``IsFalse`` constraint instead.
1168

117-
This message is shown if the underlying data is not false.
9+
.. include:: /reference/constraints/IsFalse.rst

reference/constraints/IsFalse.rst

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
IsFalse
2+
=======
3+
4+
Validates that a value is ``false``. Specifically, this checks to see if
5+
the value is exactly ``false``, exactly the integer ``0``, or exactly the
6+
string "``0``".
7+
8+
Also see :doc:`IsTrue <IsTrue>`.
9+
10+
+----------------+-----------------------------------------------------------------------+
11+
| Applies to | :ref:`property or method <validation-property-target>` |
12+
+----------------+-----------------------------------------------------------------------+
13+
| Options | - `message`_ |
14+
+----------------+-----------------------------------------------------------------------+
15+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\IsFalse` |
16+
+----------------+-----------------------------------------------------------------------+
17+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IsFalseValidator` |
18+
+----------------+-----------------------------------------------------------------------+
19+
20+
Basic Usage
21+
-----------
22+
23+
The ``IsFalse`` constraint can be applied to a property or a "getter" method,
24+
but is most commonly useful in the latter case. For example, suppose that
25+
you want to guarantee that some ``state`` property is *not* in a dynamic
26+
``invalidStates`` array. First, you'd create a "getter" method::
27+
28+
protected $state;
29+
30+
protected $invalidStates = array();
31+
32+
public function isStateInvalid()
33+
{
34+
return in_array($this->state, $this->invalidStates);
35+
}
36+
37+
In this case, the underlying object is only valid if the ``isStateInvalid``
38+
method returns **false**:
39+
40+
.. configuration-block::
41+
42+
.. code-block:: php-annotations
43+
44+
// src/AppBundle/Entity/Author.php
45+
namespace AppBundle\Entity;
46+
47+
use Symfony\Component\Validator\Constraints as Assert;
48+
49+
class Author
50+
{
51+
/**
52+
* @Assert\IsFalse(
53+
* message = "You've entered an invalid state."
54+
* )
55+
*/
56+
public function isStateInvalid()
57+
{
58+
// ...
59+
}
60+
}
61+
62+
.. code-block:: yaml
63+
64+
# src/AppBundle/Resources/config/validation.yml
65+
AppBundle\Entity\Author
66+
getters:
67+
stateInvalid:
68+
- 'IsFalse':
69+
message: You've entered an invalid state.
70+
71+
.. code-block:: xml
72+
73+
<!-- src/AppBundle/Resources/config/validation.xml -->
74+
<?xml version="1.0" encoding="UTF-8" ?>
75+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
76+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
77+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
78+
79+
<class name="AppBundle\Entity\Author">
80+
<getter property="stateInvalid">
81+
<constraint name="IsFalse">
82+
<option name="message">You've entered an invalid state.</option>
83+
</constraint>
84+
</getter>
85+
</class>
86+
</constraint-mapping>
87+
88+
.. code-block:: php
89+
90+
// src/AppBundle/Entity/Author.php
91+
namespace AppBundle\Entity;
92+
93+
use Symfony\Component\Validator\Mapping\ClassMetadata;
94+
use Symfony\Component\Validator\Constraints as Assert;
95+
96+
class Author
97+
{
98+
public static function loadValidatorMetadata(ClassMetadata $metadata)
99+
{
100+
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse());
101+
}
102+
}
103+
104+
Options
105+
-------
106+
107+
message
108+
~~~~~~~
109+
110+
**type**: ``string`` **default**: ``This value should be false.``
111+
112+
This message is shown if the underlying data is not false.

reference/constraints/IsNull.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
IsNull
2+
======
3+
4+
Validates that a value is exactly equal to ``null``. To force that a property
5+
is simply blank (blank string or ``null``), see the :doc:`/reference/constraints/Blank`
6+
constraint. To ensure that a property is not null, see :doc:`/reference/constraints/NotNull`.
7+
8+
Also see :doc:`NotNull <NotNull>`.
9+
10+
+----------------+-----------------------------------------------------------------------+
11+
| Applies to | :ref:`property or method <validation-property-target>` |
12+
+----------------+-----------------------------------------------------------------------+
13+
| Options | - `message`_ |
14+
+----------------+-----------------------------------------------------------------------+
15+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\IsNull` |
16+
+----------------+-----------------------------------------------------------------------+
17+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IsNullValidator` |
18+
+----------------+-----------------------------------------------------------------------+
19+
20+
Basic Usage
21+
-----------
22+
23+
If, for some reason, you wanted to ensure that the ``firstName`` property
24+
of an ``Author`` class exactly equal to ``null``, you could do the following:
25+
26+
.. configuration-block::
27+
28+
.. code-block:: php-annotations
29+
30+
// src/AppBundle/Entity/Author.php
31+
namespace AppBundle\Entity;
32+
33+
use Symfony\Component\Validator\Constraints as Assert;
34+
35+
class Author
36+
{
37+
/**
38+
* @Assert\IsNull()
39+
*/
40+
protected $firstName;
41+
}
42+
43+
.. code-block:: yaml
44+
45+
# src/AppBundle/Resources/config/validation.yml
46+
AppBundle\Entity\Author:
47+
properties:
48+
firstName:
49+
- 'IsNull': ~
50+
51+
.. code-block:: xml
52+
53+
<!-- src/AppBundle/Resources/config/validation.xml -->
54+
<?xml version="1.0" encoding="UTF-8" ?>
55+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
56+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
57+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
58+
59+
<class name="AppBundle\Entity\Author">
60+
<property name="firstName">
61+
<constraint name="IsNull" />
62+
</property>
63+
</class>
64+
</constraint-mapping>
65+
66+
.. code-block:: php
67+
68+
// src/AppBundle/Entity/Author.php
69+
namespace AppBundle\Entity;
70+
71+
use Symfony\Component\Validator\Mapping\ClassMetadata;
72+
use Symfony\Component\Validator\Constraints as Assert;
73+
74+
class Author
75+
{
76+
public static function loadValidatorMetadata(ClassMetadata $metadata)
77+
{
78+
$metadata->addPropertyConstraint('firstName', Assert\IsNull());
79+
}
80+
}
81+
82+
Options
83+
-------
84+
85+
message
86+
~~~~~~~
87+
88+
**type**: ``string`` **default**: ``This value should be null.``
89+
90+
This is the message that will be shown if the value is not ``null``.

0 commit comments

Comments
 (0)