Skip to content

Commit 36edead

Browse files
committed
[reference][constraints] Tweaks for new Size and SizeLength constraints - see symfony#1252
1 parent 3d642cd commit 36edead

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

reference/constraints.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ Validation Constraints Reference
1616
constraints/Email
1717
constraints/MinLength
1818
constraints/MaxLength
19+
constraints/SizeLength
1920
constraints/Url
2021
constraints/Regex
2122
constraints/Ip
2223

2324
constraints/Max
2425
constraints/Min
26+
constraints/Size
2527

2628
constraints/Date
2729
constraints/DateTime
@@ -41,8 +43,6 @@ Validation Constraints Reference
4143
constraints/All
4244
constraints/UserPassword
4345
constraints/Valid
44-
constraints/Size
45-
constraints/SizeLength
4646

4747
The Validator is designed to validate objects against *constraints*.
4848
In real life, a constraint could be: "The cake must not be burned". In

reference/constraints/Size.rst

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ the following:
2828
.. code-block:: yaml
2929
3030
# src/Acme/EventBundle/Resources/config/validation.yml
31-
Acme\EventBundle\Entity\Height:
31+
Acme\EventBundle\Entity\Participant:
3232
properties:
3333
height:
34-
- Size: { min: 120, max: 180, minMessage: You must be at least 120cm tall to enter, maxMessage: You cannot be taller than 180cm to enter }
34+
- 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
3539
3640
.. code-block:: php-annotations
3741
@@ -41,7 +45,12 @@ the following:
4145
class Participant
4246
{
4347
/**
44-
* @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")
48+
* @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"
53+
* )
4554
*/
4655
protected $height;
4756
}
@@ -63,28 +72,28 @@ max
6372
**type**: ``integer`` [:ref:`default option<validation-default-option>`]
6473

6574
This required option is the "max" value. Validation will fail if the given
66-
value is **more** than this max value.
75+
value is **greater** than this max value.
6776

6877
minMessage
6978
~~~~~~~~~~
7079

71-
**type**: ``string`` **default**: ``This value should be {{ limit }} or more``
80+
**type**: ``string`` **default**: ``This value should be {{ limit }} or more.``
7281

73-
The message that will be shown if the underlying value is less than the `limit`_
82+
The message that will be shown if the underlying value is less than the `min`_
7483
option.
7584

7685
maxMessage
7786
~~~~~~~~~~
7887

79-
**type**: ``string`` **default**: ``This value should be {{ limit }} or less``
88+
**type**: ``string`` **default**: ``This value should be {{ limit }} or less.``
8089

81-
The message that will be shown if the underlying value is more than the `limit`_
90+
The message that will be shown if the underlying value is more than the `max`_
8291
option.
8392

8493
invalidMessage
8594
~~~~~~~~~~~~~~
8695

87-
**type**: ``string`` **default**: ``This value should be a valid number``
96+
**type**: ``string`` **default**: ``This value should be a valid number.``
8897

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

reference/constraints/SizeLength.rst

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Validates that a given string length is *between* some minimum and maximum value
2121
Basic Usage
2222
-----------
2323

24-
To verify that the "firstname" field length of a class is between "2" and "50", you might add
25-
the following:
24+
To verify that the ``firstName`` field length of a class is between "2" and
25+
"50", you might add the following:
2626

2727
.. configuration-block::
2828

@@ -31,8 +31,12 @@ the following:
3131
# src/Acme/EventBundle/Resources/config/validation.yml
3232
Acme\EventBundle\Entity\Height:
3333
properties:
34-
firstname:
35-
- SizeLength: { min: 2, max: 50, minMessage: Firstname must be at least 2 characters length, maxMessage: Firstname cannot be longer than than 50 characters length }
34+
firstName:
35+
- SizeLength:
36+
min: 2
37+
max: 50
38+
minMessage: Your first name must be at least 2 characters length
39+
maxMessage: Your first name cannot be longer than than 50 characters length
3640
3741
.. code-block:: php-annotations
3842
@@ -42,9 +46,14 @@ the following:
4246
class Participant
4347
{
4448
/**
45-
* @Assert\SizeLength(min = "2", max = "50", minMessage = "Firstname must be at least 2 characters length", maxMessage="Firstname cannot be longer than than 50 characters length")
49+
* @Assert\SizeLength(
50+
* min = "2",
51+
* max = "50",
52+
* minMessage = "Your first name must be at least 2 characters length",
53+
* maxMessage="Your first name cannot be longer than than 50 characters length"
54+
* )
4655
*/
47-
protected $firstname;
56+
protected $firstName;
4857
}
4958
5059
Options
@@ -64,14 +73,17 @@ max
6473
**type**: ``integer`` [:ref:`default option<validation-default-option>`]
6574

6675
This required option is the "max" length value. Validation will fail if the given
67-
value's length is **less** than this max value.
76+
value's length is **greater** than this max value.
6877

6978
charset
7079
~~~~~~~
7180

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

74-
Charset to be used when computing value's length per the `grapheme_strlen`_ PHP function, fallback to the `mb_strlen`_ PHP function, fallback to the `strlen`_ PHP function.
83+
The charset to be used when computing value's length. The `grapheme_strlen`_ PHP
84+
function is used if available. If not, the the `mb_strlen`_ PHP function
85+
is used if available. If neither are available, the `strlen`_ PHP function
86+
is used.
7587

7688
.. _`grapheme_strlen`: http://www.php.net/manual/en/function.grapheme_strlen.php
7789
.. _`mb_strlen`: http://www.php.net/manual/en/function.mb_strlen.php
@@ -80,22 +92,23 @@ Charset to be used when computing value's length per the `grapheme_strlen`_ PHP
8092
minMessage
8193
~~~~~~~~~~
8294

83-
**type**: ``string`` **default**: ``This value is too short. It should have {{ limit }} characters or more``
95+
**type**: ``string`` **default**: ``This value is too short. It should have {{ limit }} characters or more.``
8496

85-
The message that will be shown if the underlying value's length is less than the `limit`_
97+
The message that will be shown if the underlying value's length is less than the `min`_
8698
option.
8799

88100
maxMessage
89101
~~~~~~~~~~
90102

91-
**type**: ``string`` **default**: ``This value is too long. It should have {{ limit }} characters or less``
103+
**type**: ``string`` **default**: ``This value is too long. It should have {{ limit }} characters or less.``
92104

93-
The message that will be shown if the underlying value's length is more than the `limit`_
105+
The message that will be shown if the underlying value's length is more than the `max`_
94106
option.
95107

96108
exactMessage
97-
~~~~~~~~~~~~~~
109+
~~~~~~~~~~~~
98110

99-
**type**: ``string`` **default**: ``This value should have exactly {{ limit }} characters``
111+
**type**: ``string`` **default**: ``This value should have exactly {{ limit }} characters.``
100112

101-
The message that will be shown if min and max values equal and the underlying value's length is not exactly this value
113+
The message that will be shown if min and max values are equal and the underlying
114+
value's length is not exactly this value.

reference/constraints/map.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ String Constraints
1818
* :doc:`Email </reference/constraints/Email>`
1919
* :doc:`MinLength </reference/constraints/MinLength>`
2020
* :doc:`MaxLength </reference/constraints/MaxLength>`
21+
* :doc:`SizeLength </reference/constraints/SizeLength>`
2122
* :doc:`Url </reference/constraints/Url>`
2223
* :doc:`Regex </reference/constraints/Regex>`
2324
* :doc:`Ip </reference/constraints/Ip>`
@@ -27,6 +28,7 @@ Number Constraints
2728

2829
* :doc:`Max </reference/constraints/Max>`
2930
* :doc:`Min </reference/constraints/Min>`
31+
* :doc:`Size </reference/constraints/Size>`
3032

3133
Date Constraints
3234
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)