Skip to content

Commit c8c6b90

Browse files
committed
[reference][constraints] Tweaks for #1362 - Size and Range validator updates
1 parent 162dd3d commit c8c6b90

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

reference/constraints/Range.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Range
33

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

6+
.. versionadded:: 2.1
7+
The Range constraint was added in Symfony 2.1.
8+
69
+----------------+---------------------------------------------------------------------+
710
| Applies to | :ref:`property or method<validation-property-target>` |
811
+----------------+---------------------------------------------------------------------+

reference/constraints/Size.rst

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
Size
22
====
33

4-
Validates that a given string length or collection elements count is *between* some minimum and maximum value.
4+
Validates that a given string length or collection elements count is *between*
5+
some minimum and maximum value.
6+
7+
.. versionadded:: 2.1
8+
The Size constraint was added in Symfony 2.1.
59

610
+----------------+--------------------------------------------------------------------+
711
| Applies to | :ref:`property or method<validation-property-target>` |
@@ -30,7 +34,7 @@ To verify that the ``firstName`` field length of a class is between "2" and
3034
.. code-block:: yaml
3135
3236
# src/Acme/EventBundle/Resources/config/validation.yml
33-
Acme\EventBundle\Entity\Height:
37+
Acme\EventBundle\Entity\Participant:
3438
properties:
3539
firstName:
3640
- Size:
@@ -57,6 +61,44 @@ To verify that the ``firstName`` field length of a class is between "2" and
5761
protected $firstName;
5862
}
5963
64+
Basic Usage - Collections
65+
-------------------------
66+
67+
To verify that the ``emails`` array field contains between 1 and 5 elements
68+
you might add the following:
69+
70+
.. configuration-block::
71+
72+
.. code-block:: yaml
73+
74+
# src/Acme/EventBundle/Resources/config/validation.yml
75+
Acme\EventBundle\Entity\Participant:
76+
properties:
77+
emails:
78+
- Size:
79+
min: 1
80+
max: 5
81+
minMessage: You must specify at least one email
82+
maxMessage: You cannot specify more than 5 emails
83+
84+
.. code-block:: php-annotations
85+
86+
// src/Acme/EventBundle/Entity/Participant.php
87+
use Symfony\Component\Validator\Constraints as Assert;
88+
89+
class Participant
90+
{
91+
/**
92+
* @Assert\Size(
93+
* min = "1",
94+
* max = "5",
95+
* minMessage = "You must specify at least one email",
96+
* maxMessage = "You cannot specify more than 5 emails"
97+
* )
98+
*/
99+
protected $emails = array();
100+
}
101+
60102
Options
61103
-------
62104

@@ -82,22 +124,19 @@ type
82124
**type**: ``string``
83125

84126
The type of value to validate. It can be either ``string`` or ``collection``. If
85-
not specified, the validator will try to guess it.
127+
not specified, the validator will try the correct type based on the underlying
128+
data being validated.
86129

87130
charset
88131
~~~~~~~
89132

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

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
135+
The charset to be used when computing value's length. The :phpfunction:`grapheme_strlen` PHP
136+
function is used if available. If not, the the :phpfunction:`mb_strlen` PHP function
137+
is used if available. If neither are available, the :phpfunction:`strlen` PHP function
95138
is used.
96139

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
100-
101140
minMessage
102141
~~~~~~~~~~
103142

reference/constraints/map.rst.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +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>`
21+
* :doc:`Size </reference/constraints/Size>`
2222
* :doc:`Url </reference/constraints/Url>`
2323
* :doc:`Regex </reference/constraints/Regex>`
2424
* :doc:`Ip </reference/constraints/Ip>`
@@ -28,7 +28,7 @@ Number Constraints
2828

2929
* :doc:`Max </reference/constraints/Max>`
3030
* :doc:`Min </reference/constraints/Min>`
31-
* :doc:`Size </reference/constraints/Size>`
31+
* :doc:`Range </reference/constraints/Range>`
3232

3333
Date Constraints
3434
~~~~~~~~~~~~~~~~
@@ -46,6 +46,7 @@ Collection Constraints
4646
* :doc:`Language </reference/constraints/Language>`
4747
* :doc:`Locale </reference/constraints/Locale>`
4848
* :doc:`Country </reference/constraints/Country>`
49+
* :doc:`Size </reference/constraints/Size>`
4950

5051
File Constraints
5152
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)