Skip to content

Size validator #1362

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 4 commits into from
May 20, 2012
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
4 changes: 2 additions & 2 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Validation Constraints Reference
constraints/Email
constraints/MinLength
constraints/MaxLength
constraints/SizeLength
constraints/Size
constraints/Url
constraints/Regex
constraints/Ip

constraints/Max
constraints/Min
constraints/Size
constraints/Range

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

Validates that a given number is *between* some minimum and maximum number.

+----------------+---------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+---------------------------------------------------------------------+
| Options | - `min`_ |
| | - `max`_ |
| | - `minMessage`_ |
| | - `maxMessage`_ |
| | - `invalidMessage`_ |
+----------------+---------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Range` |
+----------------+---------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\RangeValidator` |
+----------------+---------------------------------------------------------------------+

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

To verify that the "height" field of a class is between "120" and "180", you might add
the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/EventBundle/Resources/config/validation.yml
Acme\EventBundle\Entity\Participant:
properties:
height:
- Range:
min: 120
max: 180
minMessage: You must be at least 120cm tall to enter
maxMessage: You cannot be taller than 180cm to enter

.. code-block:: php-annotations

// src/Acme/EventBundle/Entity/Participant.php
use Symfony\Component\Validator\Constraints as Assert;

class Participant
{
/**
* @Assert\Range(
* min = "120",
* max = "180",
* minMessage = "You must be at least 120cm tall to enter",
* maxMessage="You cannot be taller than 180cm to enter"
* )
*/
protected $height;
}

Options
-------

min
~~~

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

This required option is the "min" value. Validation will fail if the given
value is **less** than this min value.

max
~~~

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

This required option is the "max" value. Validation will fail if the given
value is **greater** than this max value.

minMessage
~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``

The message that will be shown if the underlying value is less than the `min`_
option.

maxMessage
~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``

The message that will be shown if the underlying value is more than the `max`_
option.

invalidMessage
~~~~~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be a valid number.``

The message that will be shown if the underlying value is not a number (per
the `is_numeric`_ PHP function).

.. _`is_numeric`: http://www.php.net/manual/en/function.is-numeric.php
86 changes: 54 additions & 32 deletions reference/constraints/Size.rst
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
Size
====

Validates that a given number is *between* some minimum and maximum number.
Validates that a given string length or collection elements count is *between* some minimum and maximum value.

+----------------+--------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+--------------------------------------------------------------------+
| Options | - `min`_ |
| | - `max`_ |
| | - `type`_ |
| | - `charset`_ |
| | - `minMessage`_ |
| | - `maxMessage`_ |
| | - `invalidMessage`_ |
| | - `exactMessage`_ |
+----------------+--------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Size` |
Copy link
Contributor

Choose a reason for hiding this comment

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

missing validator ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it's just not displayed here because it was not modified.

Copy link
Contributor

Choose a reason for hiding this comment

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

right, sorry !

+----------------+--------------------------------------------------------------------+
Expand All @@ -20,22 +22,22 @@ Validates that a given number is *between* some minimum and maximum number.
Basic Usage
-----------

To verify that the "height" field of a class is between "120" and "180", you might add
the following:
To verify that the ``firstName`` field length of a class is between "2" and
"50", you might add the following:

.. configuration-block::

.. code-block:: yaml

# src/Acme/EventBundle/Resources/config/validation.yml
Acme\EventBundle\Entity\Participant:
Acme\EventBundle\Entity\Height:
properties:
height:
firstName:
- Size:
min: 120
max: 180
minMessage: You must be at least 120cm tall to enter
maxMessage: You cannot be taller than 180cm to enter
min: 2
max: 50
minMessage: Your first name must be at least 2 characters length
maxMessage: Your first name cannot be longer than than 50 characters length

.. code-block:: php-annotations

Expand All @@ -46,13 +48,13 @@ the following:
{
/**
* @Assert\Size(
* min = "120",
* max = "180",
* minMessage = "You must be at least 120cm tall to enter",
* maxMessage="You cannot be taller than 180cm to enter"
* min = "2",
* max = "50",
* minMessage = "Your first name must be at least 2 characters length",
* maxMessage = "Your first name cannot be longer than than 50 characters length"
* )
*/
protected $height;
protected $firstName;
}

Options
Expand All @@ -63,39 +65,59 @@ min

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

This required option is the "min" value. Validation will fail if the given
value is **less** than this min value.
This required option is the "min" length value. Validation will fail if the given
value's length is **less** than this min value.

max
~~~

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

This required option is the "max" value. Validation will fail if the given
value is **greater** than this max value.
This required option is the "max" length value. Validation will fail if the given
value's length is **greater** than this max value.

type
~~~~

**type**: ``string``

The type of value to validate. It can be either ``string`` or ``collection``. If
not specified, the validator will try to guess it.

charset
~~~~~~~

**type**: ``string`` **default**: ``UTF-8``

The charset to be used when computing value's length. The `grapheme_strlen`_ PHP
function is used if available. If not, the the `mb_strlen`_ PHP function
is used if available. If neither are available, the `strlen`_ PHP function
is used.

.. _`grapheme_strlen`: http://www.php.net/manual/en/function.grapheme_strlen.php
.. _`mb_strlen`: http://www.php.net/manual/en/function.mb_strlen.php
.. _`strlen`: http://www.php.net/manual/en/function.strlen.php

minMessage
Copy link
Contributor

Choose a reason for hiding this comment

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

The messages should be updated (for string / collection)

~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``
**type**: ``string`` **default**: ``This value is too short. It should have {{ limit }} characters or more.`` when validating a string, or ``This collection should contain {{ limit }} elements or more.`` when validating a collection.

The message that will be shown if the underlying value is less than the `min`_
option.
The message that will be shown if the underlying value's length or collection elements
count is less than the `min`_ option.

maxMessage
~~~~~~~~~~

**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``

The message that will be shown if the underlying value is more than the `max`_
option.
**type**: ``string`` **default**: ``This value is too long. It should have {{ limit }} characters or less.`` when validating a string, or ``This collection should contain {{ limit }} elements or less.`` when validating a collection.

invalidMessage
~~~~~~~~~~~~~~
The message that will be shown if the underlying value's length or collection elements
count is more than the `max`_ option.

**type**: ``string`` **default**: ``This value should be a valid number.``
exactMessage
~~~~~~~~~~~~

The message that will be shown if the underlying value is not a number (per
the `is_numeric`_ PHP function).
**type**: ``string`` **default**: ``This value should have exactly {{ limit }} characters.`` when validating a string, or ``This collection should contain exactly {{ limit }} elements.`` when validating a collection.

.. _`is_numeric`: http://www.php.net/manual/en/function.is-numeric.php
The message that will be shown if min and max values are equal and the underlying
value's length or collection elements count is not exactly this value.
114 changes: 0 additions & 114 deletions reference/constraints/SizeLength.rst

This file was deleted.