diff --git a/components/options_resolver.rst b/components/options_resolver.rst index 1ed2347b4d8..54fa38829ba 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -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_()`` function is defined -in PHP. Additionally, you may pass fully qualified class or interface names. +You can pass any type for which an ``is_()`` 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`