diff --git a/reference/constraints/Type.rst b/reference/constraints/Type.rst index c60e1b25b65..319387416ec 100644 --- a/reference/constraints/Type.rst +++ b/reference/constraints/Type.rst @@ -18,8 +18,10 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator` Basic Usage ----------- -This will check if ``firstName`` is of type ``string`` and that ``age`` is an -``integer``. +This will check if ``firstName`` is of type ``string`` (using :phpfunction:`is_string` +PHP function), ``age`` is an ``integer`` (using :phpfunction:`is_int` PHP +function) and ``accessCode`` contains either only letters or only digits (using +:phpfunction:`ctype_alpha` and :phpfunction:`ctype_digit` PHP functions). .. configuration-block:: @@ -44,6 +46,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an * ) */ protected $age; + + /** + * @Assert\Type(type={"alpha", "digit"}) + */ + protected $accessCode; } .. code-block:: yaml @@ -59,6 +66,10 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an type: integer message: The value {{ value }} is not a valid {{ type }}. + accessCode: + - Type: + type: [alpha, digit] + .. code-block:: xml @@ -79,6 +90,14 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an + + + + + @@ -100,9 +119,18 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an 'type' => 'integer', 'message' => 'The value {{ value }} is not a valid {{ type }}.', ])); + + $metadata->addPropertyConstraint('accessCode', new Assert\Type([ + 'type' => ['alpha', 'digit'], + ])); } } +.. versionadded:: 4.4 + + The feature to define multiple types in the ``type`` option was introduced + in Symfony 4.4. + Options ------- @@ -131,10 +159,16 @@ Parameter Description type ~~~~ -**type**: ``string`` [:ref:`default option `] +**type**: ``string`` or ``array`` [:ref:`default option `] + +.. versionadded:: 4.4 + + The feature to define multiple types in the ``type`` option was introduced + in Symfony 4.4. -This required option is the fully qualified class name or one of the PHP -datatypes as determined by PHP's ``is_()`` functions. +This required option defines the type or collection of types allowed for the +given value. Each type is defined as the fully qualified class name or one of +the PHP datatypes as determined by PHP's ``is_*()`` functions. * :phpfunction:`array ` * :phpfunction:`bool ` @@ -153,7 +187,7 @@ datatypes as determined by PHP's ``is_()`` functions. * :phpfunction:`scalar ` * :phpfunction:`string ` -Also, you can use ``ctype_()`` functions from corresponding +Also, you can use ``ctype_*()`` functions from corresponding `built-in PHP extension`_. Consider `a list of ctype functions`_: * :phpfunction:`alnum `