Skip to content

Commit c64123b

Browse files
committed
[Validator] Documented the support of multiple types in Type constraint
1 parent e0a86d1 commit c64123b

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

reference/constraints/Type.rst

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator`
1818
Basic Usage
1919
-----------
2020

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).
2325

2426
.. configuration-block::
2527

@@ -44,6 +46,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
4446
* )
4547
*/
4648
protected $age;
49+
50+
/**
51+
* @Assert\Type(type={"alpha", "digit"})
52+
*/
53+
protected $accessCode;
4754
}
4855
4956
.. code-block:: yaml
@@ -59,6 +66,10 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
5966
type: integer
6067
message: The value {{ value }} is not a valid {{ type }}.
6168
69+
accessCode:
70+
- Type:
71+
type: [alpha, digit]
72+
6273
.. code-block:: xml
6374
6475
<!-- config/validator/validation.xml -->
@@ -79,6 +90,14 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
7990
<option name="message">The value {{ value }} is not a valid {{ type }}.</option>
8091
</constraint>
8192
</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>
82101
</class>
83102
</constraint-mapping>
84103
@@ -100,9 +119,18 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
100119
'type' => 'integer',
101120
'message' => 'The value {{ value }} is not a valid {{ type }}.',
102121
]));
122+
123+
$metadata->addPropertyConstraint('accessCode', new Assert\Type([
124+
'type' => ['alpha', 'digit'],
125+
]));
103126
}
104127
}
105128
129+
.. versionadded:: 4.4
130+
131+
The feature to define multiple types in the ``type`` option was introduced
132+
in Symfony 4.4.
133+
106134
Options
107135
-------
108136

@@ -131,10 +159,16 @@ Parameter Description
131159
type
132160
~~~~
133161

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.
135168

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.
138172

139173
* :phpfunction:`array <is_array>`
140174
* :phpfunction:`bool <is_bool>`
@@ -153,7 +187,7 @@ datatypes as determined by PHP's ``is_()`` functions.
153187
* :phpfunction:`scalar <is_scalar>`
154188
* :phpfunction:`string <is_string>`
155189

156-
Also, you can use ``ctype_()`` functions from corresponding
190+
Also, you can use ``ctype_*()`` functions from corresponding
157191
`built-in PHP extension`_. Consider `a list of ctype functions`_:
158192

159193
* :phpfunction:`alnum <ctype_alnum>`

0 commit comments

Comments
 (0)