@@ -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
329342If you pass an invalid option now, an
330343:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `
@@ -340,43 +353,6 @@ is thrown::
340353In sub-classes, you can use :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedTypes `
341354to add additional allowed types without erasing the ones already set.
342355
343- You can specify an array of a specific type as an allowed option. The expected type is
344- validated in the same way as before (``is_<type>() `` or an ``instanceof `` check).
345- Only for an array, this is done recursively. If you expect an option to be an array of
346- ``DateTime `` instances or a numeric array, you can specify this as follows::
347-
348- // ...
349- class Mailer
350- {
351- // ...
352- public function configureOptions(OptionsResolver $resolver)
353- {
354- // ...
355- $resolver->setAllowedTypes('dates', 'DateTime[]');
356- $resolver->setAllowedTypes('ports', 'int[]');
357- }
358- }
359-
360- Because the OptionsResolver will validate typed arrays recursively, it is possible to
361- resolve multi-dimensional arrays, too::
362-
363- // ...
364- class Mailer
365- {
366- // ...
367- public function configureOptions(OptionsResolver $resolver)
368- {
369- // ...
370- //allowed type is a 2D array of string values
371- $resolver->setAllowedTypes('hosts', 'string[][]');
372- }
373- }
374-
375- .. versionadded :: 3.1
376- Before Symfony 3.1, the allowed types had to be scalar values, qualified classes
377- or interfaces. The only way to ensure the values of an array were of the right type
378- was to use a normalizer.
379-
380356Value Validation
381357~~~~~~~~~~~~~~~~
382358
0 commit comments