Skip to content

Documented the Comparison validators #2599

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

Merged
merged 8 commits into from
May 5, 2013
Merged
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
9 changes: 9 additions & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Validation Constraints Reference

constraints/Range

constraints/EqualTo
constraints/NotEqualTo
constraints/IdenticalTo
constraints/NotIdenticalTo
constraints/LessThan
constraints/LessThanOrEqual
constraints/GreaterThan
constraints/GreaterThanOrEqual

constraints/Date
constraints/DateTime
constraints/Time
Expand Down
100 changes: 100 additions & 0 deletions reference/constraints/EqualTo.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
EqualTo
=======

.. versionadded:: 2.3
This constraint is new in version 2.3.

Validates that a value is equal to another value, defined in the options. To
force that a value is *not* equal, see :doc:`/reference/constraints/NotEqualTo`.

.. caution::

This constraint compares using ``==``, so ``3`` and ``"3"`` are considered
equal. Use :doc:`/reference/constraints/IdenticalTo` to compare with
``===``.

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

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

If you want to ensure that the ``age`` of a ``Person`` class is equal to
``20``, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/SocialBundle/Resources/config/validation.yml
Acme\SocialBundle\Entity\Person:
properties:
age:
- EqualTo:
value: 20

.. code-block:: php-annotations

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Person
{
/**
* @Assert\EqualTo(
* value = 20
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/SocialBundle/Resources/config/validation.xml -->
<class name="Acme\SocialBundle\Entity\Person">
<property name="age">
<constraint name="EqualTo">
<option name="value">20</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

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

class Person
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\EqualTo(array(
'value' => 20,
)));
}
}

Options
-------

.. include:: /reference/constraints/_comparison-value-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be equal to {{ compared_value }}``

This is the message that will be shown if the value is not equal.
96 changes: 96 additions & 0 deletions reference/constraints/GreaterThan.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
GreaterThan
===========

.. versionadded:: 2.3
This constraint is new in version 2.3.

Validates that a value is greater than another value, defined in the options. To
force that a value is greater than or equal to another value, see
:doc:`/reference/constraints/GreaterThanOrEqual`. To force a value is less
than another value, see :doc:`/reference/constraints/LessThan`.

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

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

If you want to ensure that the ``age`` of a ``Person`` class is greater than
``18``, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/SocialBundle/Resources/config/validation.yml
Acme\SocialBundle\Entity\Person:
properties:
age:
- GreaterThan:
value: 18

.. code-block:: php-annotations

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Person
{
/**
* @Assert\GreaterThan(
* value = 18
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/SocialBundle/Resources/config/validation.xml -->
<class name="Acme\SocialBundle\Entity\Person">
<property name="age">
<constraint name="GreaterThan">
<option name="value">18</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

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

class Person
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\GreaterThan(array(
'value' => 18,
)));
}
}

Options
-------

.. include:: /reference/constraints/_comparison-value-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be greater than {{ compared_value }}``

This is the message that will be shown if the value is not equal.
95 changes: 95 additions & 0 deletions reference/constraints/GreaterThanOrEqual.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
GreaterThanOrEqual
===========

.. versionadded:: 2.3
This constraint is new in version 2.3.

Validates that a value is greater than or equal to another value, defined in
the options. To force that a value is greater than another value, see
:doc:`/reference/constraints/GreaterThan`.

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

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

If you want to ensure that the ``age`` of a ``Person`` class is greater than
or equal to ``18``, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/SocialBundle/Resources/config/validation.yml
Acme\SocialBundle\Entity\Person:
properties:
age:
- GreaterThanOrEqual:
value: 18

.. code-block:: php-annotations

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Person
{
/**
* @Assert\GreaterThanOrEqual(
* value = 18
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/SocialBundle/Resources/config/validation.xml -->
<class name="Acme\SocialBundle\Entity\Person">
<property name="age">
<constraint name="GreaterThanOrEqual">
<option name="value">18</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/SocialBundle/Entity/Person.php
namespace Acme\SocialBundle\Entity;

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

class Person
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual(array(
'value' => 18,
)));
}
}

Options
-------

.. include:: /reference/constraints/_comparison-value-option.rst.inc

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be greater than or equal to {{ compared_value }}``

This is the message that will be shown if the value is not equal.
Loading