Thanks to visit codestin.com
Credit goes to github.com

Skip to content

replacing deprecated usage of True, False, Null validators in docs #5677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ this method must return ``true``:
class Author
{
/**
* @Assert\True(message = "The password cannot match your first name")
* @Assert\IsTrue(message = "The password cannot match your first name")
*/
public function isPasswordLegal()
{
Expand Down Expand Up @@ -675,7 +675,7 @@ this method must return ``true``:
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
$metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
'message' => 'The password cannot match your first name',
)));
}
Expand Down Expand Up @@ -937,7 +937,7 @@ username and the password are different only if all other validation passes
private $password;

/**
* @Assert\True(message="The password cannot match your username", groups={"Strict"})
* @Assert\IsTrue(message="The password cannot match your username", groups={"Strict"})
*/
public function isPasswordLegal()
{
Expand Down Expand Up @@ -1011,7 +1011,7 @@ username and the password are different only if all other validation passes
$metadata->addPropertyConstraint('username', new Assert\NotBlank());
$metadata->addPropertyConstraint('password', new Assert\NotBlank());

$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
$metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
'message' => 'The password cannot match your first name',
'groups' => array('Strict'),
)));
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Validation Constraints Reference
constraints/NotBlank
constraints/Blank
constraints/NotNull
constraints/Null
constraints/True
constraints/False
constraints/IsNull
constraints/IsTrue
constraints/IsFalse
constraints/Type

constraints/Email
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Blank.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Blank

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

+----------------+---------------------------------------------------------------------+
Expand Down
117 changes: 3 additions & 114 deletions reference/constraints/False.rst
Original file line number Diff line number Diff line change
@@ -1,120 +1,9 @@
False
=====

Validates that a value is ``false``. Specifically, this checks to see if
the value is exactly ``false``, exactly the integer ``0``, or exactly the
string "``0``".

Also see :doc:`True <True>`.

+----------------+---------------------------------------------------------------------+
| Applies to | :ref:`property or method <validation-property-target>` |
+----------------+---------------------------------------------------------------------+
| Options | - `message`_ |
| | - `payload`_ |
+----------------+---------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\False` |
+----------------+---------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\FalseValidator` |
+----------------+---------------------------------------------------------------------+

Basic Usage
-----------

The ``False`` constraint can be applied to a property or a "getter" method,
but is most commonly useful in the latter case. For example, suppose that
you want to guarantee that some ``state`` property is *not* in a dynamic
``invalidStates`` array. First, you'd create a "getter" method::

protected $state;

protected $invalidStates = array();

public function isStateInvalid()
{
return in_array($this->state, $this->invalidStates);
}

In this case, the underlying object is only valid if the ``isStateInvalid``
method returns **false**:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @Assert\False(
* message = "You've entered an invalid state."
* )
*/
public function isStateInvalid()
{
// ...
}
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author
getters:
stateInvalid:
- 'False':
message: You've entered an invalid state.

.. code-block:: xml

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<getter property="stateInvalid">
<constraint name="False">
<option name="message">You've entered an invalid state.</option>
</constraint>
</getter>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('stateInvalid', new Assert\False());
}
}

.. caution::

When using YAML, be sure to surround ``False`` with quotes (``'False'``)
or else YAML will convert this into a ``false`` boolean value.

Options
-------

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be false.``

This message is shown if the underlying data is not false.
The ``False`` constraint is deprecated since Symfony 2.7
and will be removed in Symfony 3.0. Use the ``IsFalse`` constraint instead.

.. include:: /reference/constraints/_payload-option.rst.inc
.. include:: /reference/constraints/IsFalse.rst
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of including the IsFalse documentation, I prefer to add a link to this docs in the caution above. So we end up with:

.. caution::

    The ``False`` constraint is deprecated since Symfony 2.7 and will
    be removed in 3.0. Use the :doc:`/reference/constraints/IsFalse` constraint
    instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, including it does weird things, as it also includes the title

115 changes: 115 additions & 0 deletions reference/constraints/IsFalse.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
IsFalse
=======

Validates that a value is ``false``. Specifically, this checks to see if
the value is exactly ``false``, exactly the integer ``0``, or exactly the
string "``0``".

Also see :doc:`IsTrue <IsTrue>`.

+----------------+-----------------------------------------------------------------------+
| Applies to | :ref:`property or method <validation-property-target>` |
+----------------+-----------------------------------------------------------------------+
| Options | - `message`_ |
| | - `payload`_ |
+----------------+-----------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\IsFalse` |
+----------------+-----------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IsFalseValidator` |
+----------------+-----------------------------------------------------------------------+

Basic Usage
-----------

The ``IsFalse`` constraint can be applied to a property or a "getter" method,
but is most commonly useful in the latter case. For example, suppose that
you want to guarantee that some ``state`` property is *not* in a dynamic
``invalidStates`` array. First, you'd create a "getter" method::

protected $state;

protected $invalidStates = array();

public function isStateInvalid()
{
return in_array($this->state, $this->invalidStates);
}

In this case, the underlying object is only valid if the ``isStateInvalid``
method returns **false**:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @Assert\IsFalse(
* message = "You've entered an invalid state."
* )
*/
public function isStateInvalid()
{
// ...
}
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author
getters:
stateInvalid:
- 'IsFalse':
message: You've entered an invalid state.

.. code-block:: xml

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<getter property="stateInvalid">
<constraint name="IsFalse">
<option name="message">You've entered an invalid state.</option>
</constraint>
</getter>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse());
}
}

Options
-------

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be false.``

This message is shown if the underlying data is not false.

.. include:: /reference/constraints/_payload-option.rst.inc
93 changes: 93 additions & 0 deletions reference/constraints/IsNull.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
IsNull
======

Validates that a value is exactly equal to ``null``. To force that a property
is simply blank (blank string or ``null``), see the :doc:`/reference/constraints/Blank`
constraint. To ensure that a property is not null, see :doc:`/reference/constraints/NotNull`.

Also see :doc:`NotNull <NotNull>`.

+----------------+-----------------------------------------------------------------------+
| Applies to | :ref:`property or method <validation-property-target>` |
+----------------+-----------------------------------------------------------------------+
| Options | - `message`_ |
| | - `payload`_ |
+----------------+-----------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\IsNull` |
+----------------+-----------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IsNullValidator` |
+----------------+-----------------------------------------------------------------------+

Basic Usage
-----------

If, for some reason, you wanted to ensure that the ``firstName`` property
of an ``Author`` class exactly equal to ``null``, you could do the following:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Author
{
/**
* @Assert\IsNull()
*/
protected $firstName;
}

.. code-block:: yaml

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
properties:
firstName:
- 'IsNull': ~

.. code-block:: xml

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Author">
<property name="firstName">
<constraint name="IsNull" />
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', Assert\IsNull());
}
}

Options
-------

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be null.``

This is the message that will be shown if the value is not ``null``.

.. include:: /reference/constraints/_payload-option.rst.inc
Loading