Skip to content

[OptionsResolver] Documented validation of array types #8946

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
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
19 changes: 16 additions & 3 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,27 @@ correctly. To validate the types of the options, call
public function configureOptions(OptionsResolver $resolver)
{
// ...

// specify one allowed type
$resolver->setAllowedTypes('host', 'string');

// specify multiple allowed types
$resolver->setAllowedTypes('port', array('null', 'int'));

// check all items in an array recursively for a type
$resolver->setAllowedTypes('dates', 'DateTime[]');
$resolver->setAllowedtypes('ports', 'int[]');
}
}

For each option, you can define either just one type or an array of acceptable
types. You can pass any type for which an ``is_<type>()`` function is defined
in PHP. Additionally, you may pass fully qualified class or interface names.
You can pass any type for which an ``is_<type>()`` function is defined in PHP.
You may also pass fully qualified class or interface names (which is checked
using ``instanceof``). Additionally, you can validate all items in an array
recursively by suffixing the type with ``[]``.

.. versionadded:: 3.4
Validating types of array items recursively was introduced in Symfony 3.4.
Prior to Symfony 3.4, only scalar values could be validated.

If you pass an invalid option now, an
:class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException`
Expand Down