-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Size validator #1362
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7aa89c
[Validator] Rename Size into Range validator
Herzult 5e94473
[Validator] Rename SizeLength into Size & complete it
Herzult 9f6fc54
[Validator] Update Size message descriptions
Herzult 3b69ec1
[Validator] Add Size collection default messages
Herzult File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | | ||
+----------------+--------------------------------------------------------------------+ | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing validator ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, sorry !