Skip to content

Commit 38a6e7c

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

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

reference/constraints/Type.rst

Lines changed: 36 additions & 5 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,11 @@ 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"> ..... </option>
96+
</constraint>
97+
</property>
8298
</class>
8399
</constraint-mapping>
84100
@@ -100,9 +116,18 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
100116
'type' => 'integer',
101117
'message' => 'The value {{ value }} is not a valid {{ type }}.',
102118
]));
119+
120+
$metadata->addPropertyConstraint('accessCode', new Assert\Type([
121+
'type' => ['alpha', 'digit'],
122+
]));
103123
}
104124
}
105125
126+
.. versionadded:: 4.4
127+
128+
The feature to define multiple types in the ``type`` option was introduced
129+
in Symfony 4.4.
130+
106131
Options
107132
-------
108133

@@ -131,10 +156,16 @@ Parameter Description
131156
type
132157
~~~~
133158

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

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

139170
* :phpfunction:`array <is_array>`
140171
* :phpfunction:`bool <is_bool>`

0 commit comments

Comments
 (0)