Skip to content

[Validator] Documented the support of multiple types in Type constraint #11662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions reference/constraints/Type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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
Expand All @@ -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

<!-- config/validator/validation.xml -->
Expand All @@ -79,6 +90,14 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
<option name="message">The value {{ value }} is not a valid {{ type }}.</option>
</constraint>
</property>
<property name="accessCode">
<constraint name="Type">
<option name="type">
<value>alpha</value>
<value>digit</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>

Expand All @@ -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
-------

Expand Down Expand Up @@ -131,10 +159,16 @@ Parameter Description
type
~~~~

**type**: ``string`` [:ref:`default option <validation-default-option>`]
**type**: ``string`` or ``array`` [:ref:`default option <validation-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 <is_array>`
* :phpfunction:`bool <is_bool>`
Expand All @@ -153,7 +187,7 @@ datatypes as determined by PHP's ``is_()`` functions.
* :phpfunction:`scalar <is_scalar>`
* :phpfunction:`string <is_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 <ctype_alnum>`
Expand Down