1
1
Size
2
2
====
3
3
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.
5
9
6
10
+----------------+--------------------------------------------------------------------+
7
11
| 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
30
34
.. code-block :: yaml
31
35
32
36
# src/Acme/EventBundle/Resources/config/validation.yml
33
- Acme\EventBundle\Entity\Height :
37
+ Acme\EventBundle\Entity\Participant :
34
38
properties :
35
39
firstName :
36
40
- Size :
@@ -57,6 +61,44 @@ To verify that the ``firstName`` field length of a class is between "2" and
57
61
protected $firstName;
58
62
}
59
63
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
+
60
102
Options
61
103
-------
62
104
@@ -82,22 +124,19 @@ type
82
124
**type **: ``string ``
83
125
84
126
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.
86
129
87
130
charset
88
131
~~~~~~~
89
132
90
133
**type **: ``string `` **default **: ``UTF-8 ``
91
134
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
95
138
is used.
96
139
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
-
101
140
minMessage
102
141
~~~~~~~~~~
103
142
0 commit comments