diff --git a/reference/constraints/EqualTo.rst b/reference/constraints/EqualTo.rst new file mode 100644 index 00000000000..bcad9dd7a00 --- /dev/null +++ b/reference/constraints/EqualTo.rst @@ -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` | ++----------------+-----------------------------------------------------------------------+ +| 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 + + + + + + + + + + + + .. 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`] + +This required option is the comparison value. Validation will fail if the given +value doesn't equal this comparison value. + +message +~~~~~~~ + +**type**: ``string`` **default**: +``This value should be equal to {{ compared_value }}.`` + +This is the message that will be shown if the value doesn't equal the `value`_ +option. diff --git a/reference/constraints/GreaterThan.rst b/reference/constraints/GreaterThan.rst new file mode 100644 index 00000000000..cd03c684e3a --- /dev/null +++ b/reference/constraints/GreaterThan.rst @@ -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` | ++----------------+---------------------------------------------------------------------------+ +| 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 + + + + + + + + + + + + .. 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`] + +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. diff --git a/reference/constraints/GreaterThanOrEqual.rst b/reference/constraints/GreaterThanOrEqual.rst new file mode 100644 index 00000000000..142269f0cf5 --- /dev/null +++ b/reference/constraints/GreaterThanOrEqual.rst @@ -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` | ++----------------+----------------------------------------------------------------------------------+ +| 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 + + + + + + + + + + + + .. 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`] + +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. diff --git a/reference/constraints/IdenticalTo.rst b/reference/constraints/IdenticalTo.rst new file mode 100644 index 00000000000..b4d1238935d --- /dev/null +++ b/reference/constraints/IdenticalTo.rst @@ -0,0 +1,110 @@ +IdenticalTo +=========== + +.. versionadded:: 2.3 + The ``IdenticalTo`` validator was added in Symfony 2.3 + +Validates that a value is equal to some other defined value in value and +type. It is equivalent to a `` === `` comparison in PHP. + ++----------------+---------------------------------------------------------------------------+ +| 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 wanted to ensure that the ``age`` property of a ``Student`` class +is the integer 18, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/Acme/AppBundle/Resources/config/validation.yml + Acme\AppBundle\Entity\Student: + properties: + age: + - IdenticalTo: + 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\IdenticalTo( + * age = 18, + * message = "Students for this course must be exactly 18 years old" + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + + .. 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\IdenticalTo(array( + 'value' => 18, + 'message' => 'Students for this course must be exactly 18 years old' + ))); + } + } + +Options +------- + +value +~~~~~ + +**type**: ``mixed`` [:ref:`default option`] + +This required option is the comparison value. Validation will fail if the +given value doesn't equal this comparison value or the type of the given +value isn't the same as the type of the comparison value. + +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 doesn't equal the `value`_ +option or the type of the value is different to the type of the `value`_ +option. diff --git a/reference/constraints/LessThan.rst b/reference/constraints/LessThan.rst new file mode 100644 index 00000000000..cc2f1f11403 --- /dev/null +++ b/reference/constraints/LessThan.rst @@ -0,0 +1,108 @@ +LessThan +======== + +.. versionadded:: 2.3 + The ``LessThan`` validator was added in Symfony 2.3 + +Validates that a value is less than some other defined value. It is equivalent +to a `` < `` comparison in PHP. + ++----------------+------------------------------------------------------------------------+ +| 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 wanted to ensure that the ``age`` property of a ``Student`` class +is less 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: + - LessThan: + value: 18 + message: Students for this course must be under 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\LessThan( + * age = 18, + * message = "Students for this course must be under 18 years old" + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + + .. 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\LessThan(array( + 'value' => 18, + 'message' => 'Students for this course must be under 18 years old' + ))); + } + } + +Options +------- + +value +~~~~~ + +**type**: ``mixed`` [:ref:`default option`] + +This required option is the comparison value. Validation will fail if the +given value is greater than or equal to this comparison value. + +message +~~~~~~~ + +**type**: ``string`` **default**: +``This value should be less than {{ compared_value }}.`` + +This is the message that will be shown if the value is greater than or equal +to the `value`_ option. diff --git a/reference/constraints/LessThanOrEqual.rst b/reference/constraints/LessThanOrEqual.rst new file mode 100644 index 00000000000..19c5182a48a --- /dev/null +++ b/reference/constraints/LessThanOrEqual.rst @@ -0,0 +1,108 @@ +LessThanOrEqual +=============== + +.. versionadded:: 2.3 + The ``LessThanOrEqual`` validator was added in Symfony 2.3 + +Validates that a value is less than or equal to some other defined value. +It is equivalent to a `` <= `` comparison in PHP. + ++----------------+-------------------------------------------------------------------------------+ +| 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 wanted to ensure that the ``age`` property of a ``Student`` class +is less 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: + - LessThanOrEqual: + value: 18 + message: Students for this course must be 18 years old or under + + .. code-block:: php-annotations + + // src/Acme/AppBundle/Entity/Student.php + namespace Acme\AppBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Student + { + /** + * @Assert\LessThanOrEqual( + * age = 18, + * message = "Students for this course must be 18 years old or under" + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + + .. 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\LessThanOrEqual(array( + 'value' => 18, + 'message' => 'Students for this course must be 18 years old or under' + ))); + } + } + +Options +------- + +value +~~~~~ + +**type**: ``mixed`` [:ref:`default option`] + +This required option is the comparison value. Validation will fail if the +given value is greater than this comparison value. + +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 greater than the +`value`_ option. diff --git a/reference/constraints/NotEqualTo.rst b/reference/constraints/NotEqualTo.rst new file mode 100644 index 00000000000..97e37e9f591 --- /dev/null +++ b/reference/constraints/NotEqualTo.rst @@ -0,0 +1,108 @@ +NotEqualTo +========== + +.. versionadded:: 2.3 + The ``NotEqualTo`` 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` | ++----------------+--------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+--------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\NotEqualTo` | ++----------------+--------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotEqualToValidator` | ++----------------+--------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you wanted to ensure that the ``age`` property of a ``Student`` class +isn't 18, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/Acme/AppBundle/Resources/config/validation.yml + Acme\AppBundle\Entity\Student: + properties: + age: + - NotEqualTo: + value: 18 + message: "Students for this course mustn't be 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\NotEqualTo( + * age = 18, + * message = "Students for this course mustn't be 18 years old" + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + + .. 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\NotEqualTo(array( + 'value' => 18, + 'message' => 'Students for this course mustn\'t be 18 years old' + ))); + } + } + +Options +------- + +value +~~~~~ + +**type**: ``mixed`` [:ref:`default option`] + +This required option is the comparison value. Validation will fail if the given +value does equal this comparison value. + +message +~~~~~~~ + +**type**: ``string`` **default**: +``This value should not be equal to {{ compared_value }}.`` + +This is the message that will be shown if the value does equal the `value`_ +option. diff --git a/reference/constraints/NotIdenticalTo.rst b/reference/constraints/NotIdenticalTo.rst new file mode 100644 index 00000000000..ec932a2fad4 --- /dev/null +++ b/reference/constraints/NotIdenticalTo.rst @@ -0,0 +1,112 @@ +NotIdenticalTo +============== + +.. versionadded:: 2.3 + The ``NotIdenticalTo`` 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` | ++----------------+------------------------------------------------------------------------------+ +| Options | - `value`_ | +| | - `message`_ | ++----------------+------------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\NotIdenticalTo` | ++----------------+------------------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\NotIdenticalToValidator` | ++----------------+------------------------------------------------------------------------------+ + +Basic Usage +----------- + +If you wanted to ensure that the ``age`` property of a ``Student`` class +isn't the integer 18, you could do the following: + +.. configuration-block:: + + .. code-block:: yaml + + # src/Acme/AppBundle/Resources/config/validation.yml + Acme\AppBundle\Entity\Student: + properties: + age: + - NotIdenticalTo: + value: 18 + message: "Students for this course mustn't be 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\NotIdenticalTo( + * age = 18, + * message = "Students for this course mustn't be 18 years old" + * ) + */ + protected $age; + } + + .. code-block:: xml + + + + + + + + + + + + .. 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\NotIdenticalTo(array( + 'value' => 18, + 'message' => 'Students for this course mustn\'t be 18 years old' + ) + )); + } + } + +Options +------- + +value +~~~~~ + +**type**: ``mixed`` [:ref:`default option`] + +This required option is the comparison value. Validation will fail if the +given value does equal this comparison value and the given value's type +is the same. + +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 does equal the `value`_ +option and the type of value is the same as the type of the `value`_ option. diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index 5dcacdd3a95..6f430d90fd7 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -44,6 +44,20 @@ Collection Constraints * :doc:`Locale ` * :doc:`Country ` +.. versionadded:: 2.3 + The comparison validators were added in Symfony 2.3 + +Comparison Constraints +~~~~~~~~~~~~~~~~~~~~~~ +* :doc:`EqualTo ` +* :doc:`NotEqualTo ` +* :doc:`IdenticalTo ` +* :doc:`NotIdenticalTo ` +* :doc:`LessThan ` +* :doc:`LessThanOrEqual ` +* :doc:`GreaterThan ` +* :doc:`GreaterThanOrEqual ` + File Constraints ~~~~~~~~~~~~~~~~