Skip to content

Added documentation for comparison validators #2603

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 1 commit 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
108 changes: 108 additions & 0 deletions reference/constraints/EqualTo.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
EqualTo
=======

.. versionadded:: 2.3
The ``EqualTo`` validator was added in Symfony 2.3

Validates that a value is equal to some other defined value. It is equivalent
to a `` == `` comparison in PHP.

+----------------+-----------------------------------------------------------------------+
| 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 wanted to ensure that the ``age`` property of a ``Student`` class
is 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- EqualTo:
value: 18
message: Students for this course must be exactly 18 years old

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\EqualTo(
* age = 18,
* message = "Students for this course must be exactly 18 years old"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="EqualTo">
<option name="value">18</option>
<option name="message">
Students for this course must be exactly 18 years old
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\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('age', new Assert\EqualTo(array(
'value' => 18,
'message' => 'Students for this course must be exactly 18 years old'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the given
value doesn't equal this comparison value.
Copy link
Member

Choose a reason for hiding this comment

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

this option is always the same. Could you please use includes, like I did in my PR? That makes it easier to maintain


message
~~~~~~~

**type**: ``string`` **default**:
``This value should be equal to {{ compared_value }}.``
Copy link
Member

Choose a reason for hiding this comment

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

please put this right after the default:, even if it breakes our line length rul.


This is the message that will be shown if the value doesn't equal the `value`_
option.
108 changes: 108 additions & 0 deletions reference/constraints/GreaterThan.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
GreaterThan
===========

.. versionadded:: 2.3
The ``GreaterThan`` validator was added in Symfony 2.3

Validates that a value is greater than some other defined value. It is equivalent
to a `` > `` comparison in PHP.

+----------------+---------------------------------------------------------------------------+
| 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 wanted to ensure that the ``age`` property of a ``Student`` class
is greater than 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- GreaterThan:
value: 18
message: Students for this course must be over 18 years old

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\GreaterThan(
* age = 18,
* message = "Students for this course must be over 18 years old"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="GreaterThan">
<option name="value">18</option>
<option name="message">
Students for this course must be over 18 years old
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\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('age', new Assert\GreaterThan(array(
'value' => 18,
'message' => 'Students for this course must be over 18 years old'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the
given value is less than or equal to this comparison value.

message
~~~~~~~

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

This is the message that will be shown if the value is less than or equal
to the `value`_ option.
108 changes: 108 additions & 0 deletions reference/constraints/GreaterThanOrEqual.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
GreaterThanOrEqual
==================

.. versionadded:: 2.3
The ``GreaterThanOrEqual`` validator was added in Symfony 2.3

Validates that a value is greater than or equal to some other defined value.
It is equivalent to a `` >= `` comparison in PHP.

+----------------+----------------------------------------------------------------------------------+
| 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 wanted to ensure that the ``age`` property of a ``Student`` class
is greater than or equal to 18, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/AppBundle/Resources/config/validation.yml
Acme\AppBundle\Entity\Student:
properties:
age:
- GreaterThanOrEqual:
value: 18
message: Students for this course must be 18 years old or over

.. code-block:: php-annotations

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
/**
* @Assert\GreaterThanOrEqual(
* age = 18,
* message = "Students for this course must be 18 years old or over"
* )
*/
protected $age;
}

.. code-block:: xml

<!-- src/Acme/AppBundle/Resources/config/validation.xml -->
<class name="Acme\AppBundle\Entity\Student">
<property name="age">
<constraint name="GreaterThanOrEqual">
<option name="value">18</option>
<option name="message">
Students for this course must be 18 years old or over
</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/AppBundle/Entity/Student.php
namespace Acme\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('age', new Assert\GreaterThanOrEqual(array(
'value' => 18,
'message' => 'Students for this course must be 18 years old or over'
)));
}
}

Options
-------

value
~~~~~

**type**: ``mixed`` [:ref:`default option<validation-default-option>`]

This required option is the comparison value. Validation will fail if the
given value is less than this comparison value.

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 less than the `value`_
option.
Loading