@@ -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,11 @@ 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" > ..... </option >
96
+ </constraint >
97
+ </property >
82
98
</class >
83
99
</constraint-mapping >
84
100
@@ -100,9 +116,18 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
100
116
'type' => 'integer',
101
117
'message' => 'The value {{ value }} is not a valid {{ type }}.',
102
118
]));
119
+
120
+ $metadata->addPropertyConstraint('accessCode', new Assert\Type([
121
+ 'type' => ['alpha', 'digit'],
122
+ ]));
103
123
}
104
124
}
105
125
126
+ .. versionadded :: 4.4
127
+
128
+ The feature to define multiple types in the ``type `` option was introduced
129
+ in Symfony 4.4.
130
+
106
131
Options
107
132
-------
108
133
@@ -131,10 +156,16 @@ Parameter Description
131
156
type
132
157
~~~~
133
158
134
- **type **: ``string `` [:ref: `default option <validation-default-option >`]
159
+ **type **: ``string `` or ``array `` [:ref: `default option <validation-default-option >`]
160
+
161
+ .. versionadded :: 4.4
162
+
163
+ The feature to define multiple types in the ``type `` option was introduced
164
+ in Symfony 4.4.
135
165
136
- This required option is the fully qualified class name or one of the PHP
137
- datatypes as determined by PHP's ``is_() `` functions.
166
+ This required option defines the type or collection of types allowed for the
167
+ given value. Each type is defined as the fully qualified class name or one of
168
+ the PHP datatypes as determined by PHP's ``is_() `` functions.
138
169
139
170
* :phpfunction: `array <is_array> `
140
171
* :phpfunction: `bool <is_bool> `
0 commit comments