Skip to content

Commit 162dd3d

Browse files
committed
Merge pull request #1362 from Herzult/size_validator
Size validator
2 parents 8fd3422 + 3b69ec1 commit 162dd3d

File tree

4 files changed

+157
-148
lines changed

4 files changed

+157
-148
lines changed

reference/constraints.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Validation Constraints Reference
1616
constraints/Email
1717
constraints/MinLength
1818
constraints/MaxLength
19-
constraints/SizeLength
19+
constraints/Size
2020
constraints/Url
2121
constraints/Regex
2222
constraints/Ip
2323

2424
constraints/Max
2525
constraints/Min
26-
constraints/Size
26+
constraints/Range
2727

2828
constraints/Date
2929
constraints/DateTime

reference/constraints/Range.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Range
2+
=====
3+
4+
Validates that a given number is *between* some minimum and maximum number.
5+
6+
+----------------+---------------------------------------------------------------------+
7+
| Applies to | :ref:`property or method<validation-property-target>` |
8+
+----------------+---------------------------------------------------------------------+
9+
| Options | - `min`_ |
10+
| | - `max`_ |
11+
| | - `minMessage`_ |
12+
| | - `maxMessage`_ |
13+
| | - `invalidMessage`_ |
14+
+----------------+---------------------------------------------------------------------+
15+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Range` |
16+
+----------------+---------------------------------------------------------------------+
17+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\RangeValidator` |
18+
+----------------+---------------------------------------------------------------------+
19+
20+
Basic Usage
21+
-----------
22+
23+
To verify that the "height" field of a class is between "120" and "180", you might add
24+
the following:
25+
26+
.. configuration-block::
27+
28+
.. code-block:: yaml
29+
30+
# src/Acme/EventBundle/Resources/config/validation.yml
31+
Acme\EventBundle\Entity\Participant:
32+
properties:
33+
height:
34+
- Range:
35+
min: 120
36+
max: 180
37+
minMessage: You must be at least 120cm tall to enter
38+
maxMessage: You cannot be taller than 180cm to enter
39+
40+
.. code-block:: php-annotations
41+
42+
// src/Acme/EventBundle/Entity/Participant.php
43+
use Symfony\Component\Validator\Constraints as Assert;
44+
45+
class Participant
46+
{
47+
/**
48+
* @Assert\Range(
49+
* min = "120",
50+
* max = "180",
51+
* minMessage = "You must be at least 120cm tall to enter",
52+
* maxMessage="You cannot be taller than 180cm to enter"
53+
* )
54+
*/
55+
protected $height;
56+
}
57+
58+
Options
59+
-------
60+
61+
min
62+
~~~
63+
64+
**type**: ``integer`` [:ref:`default option<validation-default-option>`]
65+
66+
This required option is the "min" value. Validation will fail if the given
67+
value is **less** than this min value.
68+
69+
max
70+
~~~
71+
72+
**type**: ``integer`` [:ref:`default option<validation-default-option>`]
73+
74+
This required option is the "max" value. Validation will fail if the given
75+
value is **greater** than this max value.
76+
77+
minMessage
78+
~~~~~~~~~~
79+
80+
**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``
81+
82+
The message that will be shown if the underlying value is less than the `min`_
83+
option.
84+
85+
maxMessage
86+
~~~~~~~~~~
87+
88+
**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``
89+
90+
The message that will be shown if the underlying value is more than the `max`_
91+
option.
92+
93+
invalidMessage
94+
~~~~~~~~~~~~~~
95+
96+
**type**: ``string`` **default**: ``This value should be a valid number.``
97+
98+
The message that will be shown if the underlying value is not a number (per
99+
the `is_numeric`_ PHP function).
100+
101+
.. _`is_numeric`: http://www.php.net/manual/en/function.is-numeric.php

reference/constraints/Size.rst

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
Size
22
====
33

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

66
+----------------+--------------------------------------------------------------------+
77
| Applies to | :ref:`property or method<validation-property-target>` |
88
+----------------+--------------------------------------------------------------------+
99
| Options | - `min`_ |
1010
| | - `max`_ |
11+
| | - `type`_ |
12+
| | - `charset`_ |
1113
| | - `minMessage`_ |
1214
| | - `maxMessage`_ |
13-
| | - `invalidMessage`_ |
15+
| | - `exactMessage`_ |
1416
+----------------+--------------------------------------------------------------------+
1517
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Size` |
1618
+----------------+--------------------------------------------------------------------+
@@ -20,22 +22,22 @@ Validates that a given number is *between* some minimum and maximum number.
2022
Basic Usage
2123
-----------
2224

23-
To verify that the "height" field of a class is between "120" and "180", you might add
24-
the following:
25+
To verify that the ``firstName`` field length of a class is between "2" and
26+
"50", you might add the following:
2527

2628
.. configuration-block::
2729

2830
.. code-block:: yaml
2931
3032
# src/Acme/EventBundle/Resources/config/validation.yml
31-
Acme\EventBundle\Entity\Participant:
33+
Acme\EventBundle\Entity\Height:
3234
properties:
33-
height:
35+
firstName:
3436
- Size:
35-
min: 120
36-
max: 180
37-
minMessage: You must be at least 120cm tall to enter
38-
maxMessage: You cannot be taller than 180cm to enter
37+
min: 2
38+
max: 50
39+
minMessage: Your first name must be at least 2 characters length
40+
maxMessage: Your first name cannot be longer than than 50 characters length
3941
4042
.. code-block:: php-annotations
4143
@@ -46,13 +48,13 @@ the following:
4648
{
4749
/**
4850
* @Assert\Size(
49-
* min = "120",
50-
* max = "180",
51-
* minMessage = "You must be at least 120cm tall to enter",
52-
* maxMessage="You cannot be taller than 180cm to enter"
51+
* min = "2",
52+
* max = "50",
53+
* minMessage = "Your first name must be at least 2 characters length",
54+
* maxMessage = "Your first name cannot be longer than than 50 characters length"
5355
* )
5456
*/
55-
protected $height;
57+
protected $firstName;
5658
}
5759
5860
Options
@@ -63,39 +65,59 @@ min
6365

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

66-
This required option is the "min" value. Validation will fail if the given
67-
value is **less** than this min value.
68+
This required option is the "min" length value. Validation will fail if the given
69+
value's length is **less** than this min value.
6870

6971
max
7072
~~~
7173

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

74-
This required option is the "max" value. Validation will fail if the given
75-
value is **greater** than this max value.
76+
This required option is the "max" length value. Validation will fail if the given
77+
value's length is **greater** than this max value.
78+
79+
type
80+
~~~~
81+
82+
**type**: ``string``
83+
84+
The type of value to validate. It can be either ``string`` or ``collection``. If
85+
not specified, the validator will try to guess it.
86+
87+
charset
88+
~~~~~~~
89+
90+
**type**: ``string`` **default**: ``UTF-8``
91+
92+
The charset to be used when computing value's length. The `grapheme_strlen`_ PHP
93+
function is used if available. If not, the the `mb_strlen`_ PHP function
94+
is used if available. If neither are available, the `strlen`_ PHP function
95+
is used.
96+
97+
.. _`grapheme_strlen`: http://www.php.net/manual/en/function.grapheme_strlen.php
98+
.. _`mb_strlen`: http://www.php.net/manual/en/function.mb_strlen.php
99+
.. _`strlen`: http://www.php.net/manual/en/function.strlen.php
76100

77101
minMessage
78102
~~~~~~~~~~
79103

80-
**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``
104+
**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.
81105

82-
The message that will be shown if the underlying value is less than the `min`_
83-
option.
106+
The message that will be shown if the underlying value's length or collection elements
107+
count is less than the `min`_ option.
84108

85109
maxMessage
86110
~~~~~~~~~~
87111

88-
**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``
89-
90-
The message that will be shown if the underlying value is more than the `max`_
91-
option.
112+
**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.
92113

93-
invalidMessage
94-
~~~~~~~~~~~~~~
114+
The message that will be shown if the underlying value's length or collection elements
115+
count is more than the `max`_ option.
95116

96-
**type**: ``string`` **default**: ``This value should be a valid number.``
117+
exactMessage
118+
~~~~~~~~~~~~
97119

98-
The message that will be shown if the underlying value is not a number (per
99-
the `is_numeric`_ PHP function).
120+
**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.
100121

101-
.. _`is_numeric`: http://www.php.net/manual/en/function.is-numeric.php
122+
The message that will be shown if min and max values are equal and the underlying
123+
value's length or collection elements count is not exactly this value.

reference/constraints/SizeLength.rst

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)