diff --git a/reference/constraints.rst b/reference/constraints.rst index 1ed9b35649b..7c6879f664e 100644 --- a/reference/constraints.rst +++ b/reference/constraints.rst @@ -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 diff --git a/reference/constraints/EqualTo.rst b/reference/constraints/EqualTo.rst new file mode 100644 index 00000000000..f698223e721 --- /dev/null +++ b/reference/constraints/EqualTo.rst @@ -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` | ++----------------+-----------------------------------------------------------------------+ +| 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 + + + + + + + + + + + .. 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. diff --git a/reference/constraints/GreaterThan.rst b/reference/constraints/GreaterThan.rst new file mode 100644 index 00000000000..3d77bb1319a --- /dev/null +++ b/reference/constraints/GreaterThan.rst @@ -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` | ++----------------+---------------------------------------------------------------------------+ +| 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 + + + + + + + + + + + .. 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. diff --git a/reference/constraints/GreaterThanOrEqual.rst b/reference/constraints/GreaterThanOrEqual.rst new file mode 100644 index 00000000000..536feb54de2 --- /dev/null +++ b/reference/constraints/GreaterThanOrEqual.rst @@ -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` | ++----------------+----------------------------------------------------------------------------------+ +| 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 + + + + + + + + + + + .. 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. diff --git a/reference/constraints/IdenticalTo.rst b/reference/constraints/IdenticalTo.rst new file mode 100644 index 00000000000..8a4aa080b55 --- /dev/null +++ b/reference/constraints/IdenticalTo.rst @@ -0,0 +1,101 @@ +IdenticalTo +=========== + +.. versionadded:: 2.3 + This constraint is new in version 2.3. + +Validates that a value is identical to another value, defined in the options. +To force that a value is *not* equal, see +:doc:`/reference/constraints/NotIdenticalTo`. + +.. caution:: + + This constraint compares using ``===``, so ``3`` and ``"3"`` are *not* + considered equal. Use :doc:`/reference/constraints/EqualTo` to compare + with ``==``. + ++----------------+--------------------------------------------------------------------------+ +| Applies to | :ref:`property or method` | ++----------------+--------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+--------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\IdenticalTo` | ++----------------+--------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IdenticalToValidator`| ++----------------+--------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you want to ensure that the ``age`` of a ``Person`` class is equal to +``20`` and an integer, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/SocialBundle/Resources/config/validation.yml + Acme\SocialBundle\Entity\Person: + properties: + age: + - IdenticalTo: + 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\IdenticalTo( + * value = 20 + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + .. 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\IdenticalTo(array( + 'value' => 20, + ))); + } + } + +Options +------- + +.. include:: /reference/constraints/_comparison-value-option.rst.inc + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should be identical to {{ compared_value_type }} {{ compared_value }}`` + +This is the message that will be shown if the value is not equal. diff --git a/reference/constraints/LessThan.rst b/reference/constraints/LessThan.rst new file mode 100644 index 00000000000..eef2393d2eb --- /dev/null +++ b/reference/constraints/LessThan.rst @@ -0,0 +1,96 @@ +LessThan +======== + +.. versionadded:: 2.3 + This constraint is new in version 2.3. + +Validates that a value is less than another value, defined in the options. To +force that a value is less than or equal to another value, see +:doc:`/reference/constraints/LessThanOrEqual`. To force a value is greater +than another value, see :doc:`/reference/constraints/GreaterThan`. + ++----------------+------------------------------------------------------------------------+ +| Applies to | :ref:`property or method` | ++----------------+------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\LessThan` | ++----------------+------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\LessThanValidator` | ++----------------+------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you want to ensure that the ``age`` of a ``Person`` class is less than +``80``, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/SocialBundle/Resources/config/validation.yml + Acme\SocialBundle\Entity\Person: + properties: + age: + - LessThan: + value: 80 + + .. code-block:: php-annotations + + // src/Acme/SocialBundle/Entity/Person.php + namespace Acme\SocialBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Person + { + /** + * @Assert\LessThan( + * value = 80 + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + .. 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\LessThan(array( + 'value' => 80, + ))); + } + } + +Options +------- + +.. include:: /reference/constraints/_comparison-value-option.rst.inc + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should be less than {{ compared_value }}`` + +This is the message that will be shown if the value is not equal. diff --git a/reference/constraints/LessThanOrEqual.rst b/reference/constraints/LessThanOrEqual.rst new file mode 100644 index 00000000000..0f4ea99c37c --- /dev/null +++ b/reference/constraints/LessThanOrEqual.rst @@ -0,0 +1,95 @@ +LessThanOrEqual +=============== + +.. versionadded:: 2.3 + This constraint is new in version 2.3. + +Validates that a value is less than or equal to another value, defined in the +options. To force that a value is less than another value, see +:doc:`/reference/constraints/LessThan`. + ++----------------+-------------------------------------------------------------------------------+ +| Applies to | :ref:`property or method` | ++----------------+-------------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+-------------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\LessThanOrEqual` | ++----------------+-------------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\LessThanOrEqualValidator` | ++----------------+-------------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you want to ensure that the ``age`` of a ``Person`` class is less than or +equal to ``80``, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/SocialBundle/Resources/config/validation.yml + Acme\SocialBundle\Entity\Person: + properties: + age: + - LessThanOrEqual: + value: 80 + + .. code-block:: php-annotations + + // src/Acme/SocialBundle/Entity/Person.php + namespace Acme\SocialBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Person + { + /** + * @Assert\LessThanOrEqual( + * value = 80 + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + .. 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\LessThanOrEqual(array( + 'value' => 80, + ))); + } + } + +Options +------- + +.. include:: /reference/constraints/_comparison-value-option.rst.inc + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should be less than or equal to {{ compared_value }}`` + +This is the message that will be shown if the value is not equal. diff --git a/reference/constraints/NotEqualTo.rst b/reference/constraints/NotEqualTo.rst new file mode 100644 index 00000000000..bea2bf6eb38 --- /dev/null +++ b/reference/constraints/NotEqualTo.rst @@ -0,0 +1,101 @@ +NotEqualTo +========== + +.. versionadded:: 2.3 + This constraint is new in version 2.3. + +Validates that a value is **not** equal to another value, defined in the +options. To force that a value is equal, see +:doc:`/reference/constraints/EqualTo`. + +.. caution:: + + This constraint compares using ``!=``, so ``3`` and ``"3"`` are considered + equal. Use :doc:`/reference/constraints/NotIdenticalTo` to compare with + ``!==``. + ++----------------+-------------------------------------------------------------------------+ +| Applies to | :ref:`property or method` | ++----------------+-------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+-------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\NotEqualTo` | ++----------------+-------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotEqualToValidator`| ++----------------+-------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you want to ensure that the ``age`` of a ``Person`` class is not equal to +``15``, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/SocialBundle/Resources/config/validation.yml + Acme\SocialBundle\Entity\Person: + properties: + age: + - NotEqualTo: + value: 15 + + .. code-block:: php-annotations + + // src/Acme/SocialBundle/Entity/Person.php + namespace Acme\SocialBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Person + { + /** + * @Assert\NotEqualTo( + * value = 15 + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + .. 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\NotEqualTo(array( + 'value' => 15, + ))); + } + } + +Options +------- + +.. include:: /reference/constraints/_comparison-value-option.rst.inc + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should not be equal to {{ compared_value }}`` + +This is the message that will be shown if the value is not equal. diff --git a/reference/constraints/NotIdenticalTo.rst b/reference/constraints/NotIdenticalTo.rst new file mode 100644 index 00000000000..2f659cbdbc5 --- /dev/null +++ b/reference/constraints/NotIdenticalTo.rst @@ -0,0 +1,101 @@ +NotIdenticalTo +=========== + +.. versionadded:: 2.3 + This constraint is new in version 2.3. + +Validates that a value is **not** identical to another value, defined in the +options. To force that a value is identical, see +:doc:`/reference/constraints/IdenticalTo`. + +.. caution:: + + This constraint compares using ``!==``, so ``3`` and ``"3"`` are + considered not equal. Use :doc:`/reference/constraints/NotEqualTo` to compare + with ``!=``. + ++----------------+-----------------------------------------------------------------------------+ +| Applies to | :ref:`property or method` | ++----------------+-----------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+-----------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\NotIdenticalTo` | ++----------------+-----------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotIdenticalToValidator`| ++----------------+-----------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you want to ensure that the ``age`` of a ``Person`` class is *not* equal to +``15`` and *not* an integer, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/SocialBundle/Resources/config/validation.yml + Acme\SocialBundle\Entity\Person: + properties: + age: + - NotIdenticalTo: + value: 15 + + .. code-block:: php-annotations + + // src/Acme/SocialBundle/Entity/Person.php + namespace Acme\SocialBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Person + { + /** + * @Assert\NotIdenticalTo( + * value = 15 + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + .. 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\NotIdenticalTo(array( + 'value' => 15, + ))); + } + } + +Options +------- + +.. include:: /reference/constraints/_comparison-value-option.rst.inc + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should not be identical to {{ compared_value_type }} {{ compared_value }}`` + +This is the message that will be shown if the value is not equal. diff --git a/reference/constraints/_comparison-value-option.rst.inc b/reference/constraints/_comparison-value-option.rst.inc new file mode 100644 index 00000000000..9d799c27dc7 --- /dev/null +++ b/reference/constraints/_comparison-value-option.rst.inc @@ -0,0 +1,7 @@ +value +~~~~~ + +**type**: ``mixed`` + +This option is required. It defines the value to compare to. It can be a +string, number or object. diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index 3d10d637ccf..1933cf5d950 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -26,6 +26,18 @@ Number Constraints * :doc:`Range ` +Comparison Constraints +~~~~~~~~~~~~~~~~~~~~~~ + +* :doc:`EqualTo ` +* :doc:`NotEqualTo ` +* :doc:`IdenticalTo ` +* :doc:`NotIdenticalTo ` +* :doc:`LessThan ` +* :doc:`LessThanOrEqual ` +* :doc:`GreaterThan ` +* :doc:`GreaterThanOrEqual ` + Date Constraints ~~~~~~~~~~~~~~~~