@@ -18,8 +18,10 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator`
18
18
Basic Usage
19
19
-----------
20
20
21
- This will check if ``firstName `` is of type ``string `` and that ``age `` is an
22
- ``integer ``.
21
+ This will check if ``firstName `` is of type ``string `` (using :phpfunction: `is_string `
22
+ PHP function), ``age `` is an ``integer `` (using :phpfunction: `is_int ` PHP
23
+ function) and ``accessCode `` contains either only letters or only digits (using
24
+ :phpfunction: `ctype_alpha ` and :phpfunction: `ctype_digit ` PHP functions).
23
25
24
26
.. configuration-block ::
25
27
@@ -44,6 +46,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
44
46
* )
45
47
*/
46
48
protected $age;
49
+
50
+ /**
51
+ * @Assert\Type(type={"alpha", "digit"})
52
+ */
53
+ protected $accessCode;
47
54
}
48
55
49
56
.. code-block :: yaml
@@ -59,6 +66,10 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
59
66
type : integer
60
67
message : The value {{ value }} is not a valid {{ type }}.
61
68
69
+ accessCode :
70
+ - Type :
71
+ type : [alpha, digit]
72
+
62
73
.. code-block :: xml
63
74
64
75
<!-- config/validator/validation.xml -->
@@ -79,6 +90,14 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
79
90
<option name =" message" >The value {{ value }} is not a valid {{ type }}.</option >
80
91
</constraint >
81
92
</property >
93
+ <property name =" accessCode" >
94
+ <constraint name =" Type" >
95
+ <option name =" type" >
96
+ <value >alpha</value >
97
+ <value >digit</value >
98
+ </option >
99
+ </constraint >
100
+ </property >
82
101
</class >
83
102
</constraint-mapping >
84
103
@@ -100,9 +119,18 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
100
119
'type' => 'integer',
101
120
'message' => 'The value {{ value }} is not a valid {{ type }}.',
102
121
]));
122
+
123
+ $metadata->addPropertyConstraint('accessCode', new Assert\Type([
124
+ 'type' => ['alpha', 'digit'],
125
+ ]));
103
126
}
104
127
}
105
128
129
+ .. versionadded :: 4.4
130
+
131
+ The feature to define multiple types in the ``type `` option was introduced
132
+ in Symfony 4.4.
133
+
106
134
Options
107
135
-------
108
136
@@ -131,10 +159,16 @@ Parameter Description
131
159
type
132
160
~~~~
133
161
134
- **type **: ``string `` [:ref: `default option <validation-default-option >`]
162
+ **type **: ``string `` or ``array `` [:ref: `default option <validation-default-option >`]
163
+
164
+ .. versionadded :: 4.4
165
+
166
+ The feature to define multiple types in the ``type `` option was introduced
167
+ in Symfony 4.4.
135
168
136
- This required option is the fully qualified class name or one of the PHP
137
- datatypes as determined by PHP's ``is_() `` functions.
169
+ This required option defines the type or collection of types allowed for the
170
+ given value. Each type is defined as the fully qualified class name or one of
171
+ the PHP datatypes as determined by PHP's ``is_*() `` functions.
138
172
139
173
* :phpfunction: `array <is_array> `
140
174
* :phpfunction: `bool <is_bool> `
@@ -153,7 +187,7 @@ datatypes as determined by PHP's ``is_()`` functions.
153
187
* :phpfunction: `scalar <is_scalar> `
154
188
* :phpfunction: `string <is_string> `
155
189
156
- Also, you can use ``ctype_() `` functions from corresponding
190
+ Also, you can use ``ctype_* () `` functions from corresponding
157
191
`built-in PHP extension `_. Consider `a list of ctype functions `_:
158
192
159
193
* :phpfunction: `alnum <ctype_alnum> `
0 commit comments