Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8fab928

Browse files
committed
minor #8946 [OptionsResolver] Documented validation of array types (EVODelavega, wouterj)
This PR was merged into the 3.4 branch. Discussion ---------- [OptionsResolver] Documented validation of array types This continues #6048 . The feature was finally merged into symfony 3.4 (congratz @EVODelavega!), so let's document it! Related Symfony PR: symfony/symfony#17032 Commits ------- cb12592 Merged new example in older example, removing some text 318e199 Fix typo bf5d9d3 Document typed arrays in optionsresolver
2 parents 936d239 + cb12592 commit 8fab928

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

components/options_resolver.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,27 @@ correctly. To validate the types of the options, call
317317
public function configureOptions(OptionsResolver $resolver)
318318
{
319319
// ...
320+
321+
// specify one allowed type
320322
$resolver->setAllowedTypes('host', 'string');
323+
324+
// specify multiple allowed types
321325
$resolver->setAllowedTypes('port', array('null', 'int'));
326+
327+
// check all items in an array recursively for a type
328+
$resolver->setAllowedTypes('dates', 'DateTime[]');
329+
$resolver->setAllowedtypes('ports', 'int[]');
322330
}
323331
}
324332

325-
For each option, you can define either just one type or an array of acceptable
326-
types. You can pass any type for which an ``is_<type>()`` function is defined
327-
in PHP. Additionally, you may pass fully qualified class or interface names.
333+
You can pass any type for which an ``is_<type>()`` function is defined in PHP.
334+
You may also pass fully qualified class or interface names (which is checked
335+
using ``instanceof``). Additionally, you can validate all items in an array
336+
recursively by suffixing the type with ``[]``.
337+
338+
.. versionadded:: 3.4
339+
Validating types of array items recursively was introduced in Symfony 3.4.
340+
Prior to Symfony 3.4, only scalar values could be validated.
328341

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

0 commit comments

Comments
 (0)