From de86e70c9e28ad86460f4f0ce474530f7252846f Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Fri, 18 Dec 2015 17:55:18 +0000 Subject: [PATCH 1/2] Document typed arrays in optionsresolver --- components/options_resolver.rst | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/components/options_resolver.rst b/components/options_resolver.rst index 436d54e5109..61aa15ffd96 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -367,6 +367,43 @@ to add additional allowed types without erasing the ones already set. the values to be given as an array mapping option names to allowed types: ``$resolver->setAllowedTypes(array('port' => array('null', 'int')));`` +You can specify an array of a specific type as an allowed option. The expected type is +validated in the same way as before (``is_()`` or an ``instanceof`` check). +Only for an array, this is done recursively. If you expect an option to be an array of +``DateTime`` instances or a numeric array, you can specify this as follows:: + + // ... + class Mailer + { + // ... + public function configureOptions(OptionsResolver $resolver) + { + // ... + $resolver->setAllowedTypes('dates', 'DateTime[]'); + $resolver->setAllowedTypes('ports', 'int[]'); + } + } + +Because the OptionsResolver will validate typed arrays recurively, it is possible to +resolve multi-dimensional arrays, too:: + + // ... + class Mailer + { + // ... + public function configureOptions(OptionsResolver $resolver) + { + // ... + //allowed type is a 2D array of string values + $resolver->setAllowedTypes('hosts', 'string[][]'); + } + } + +.. versionadded:: 3.1 + Before Symfony 3.1, the allowed types had to be scalar values, qualified classes + or interfaces. The only way to ensure the values of an array were of the right type + was to use a normalizer. + Value Validation ~~~~~~~~~~~~~~~~ From 9ec14bffcb4cdedcbe734bd3a2ec117d7a09d506 Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Mon, 21 Dec 2015 09:54:27 +0000 Subject: [PATCH 2/2] Fix typo --- components/options_resolver.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/options_resolver.rst b/components/options_resolver.rst index 61aa15ffd96..9d9e50b9a03 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -384,7 +384,7 @@ Only for an array, this is done recursively. If you expect an option to be an ar } } -Because the OptionsResolver will validate typed arrays recurively, it is possible to +Because the OptionsResolver will validate typed arrays recursively, it is possible to resolve multi-dimensional arrays, too:: // ...