@@ -717,6 +717,55 @@ In same way, parent options can access to the nested options as normal arrays::
717
717
The fact that an option is defined as nested means that you must pass
718
718
an array of values to resolve it at runtime.
719
719
720
+ Prototype Options
721
+ ~~~~~~~~~~~~~~~~~
722
+
723
+ There are situations where you will have to resolve and validate a set of
724
+ options that may repeat many times within another option. Let's imagine a
725
+ ``connections `` option that will accept an array of database connections
726
+ with ``host ``, ``database ``, ``user `` and ``password `` each.
727
+
728
+ To achieve it, you can establish the nested definition of this ``connections ``
729
+ option as prototype::
730
+
731
+ $resolver->setDefault('connections', function (OptionsResolver $connResolver) {
732
+ $connResolver
733
+ ->setPrototype(true)
734
+ ->setRequired(['host', 'database'])
735
+ ->setDefaults(['user' => 'root', 'password' => null]);
736
+ });
737
+
738
+ According to the prototype definition in the example above, it is possible
739
+ to have multiple connection arrays like following::
740
+
741
+ $resolver->resolve([
742
+ 'connections' => [
743
+ 'default' => [
744
+ 'host' => '127.0.0.1',
745
+ 'database' => 'symfony',
746
+ ],
747
+ 'test' => [
748
+ 'host' => '127.0.0.1',
749
+ 'database' => 'symfony_test',
750
+ 'user' => 'test',
751
+ 'password' => 'test',
752
+ ],
753
+ // ...
754
+ ],
755
+ ]);
756
+
757
+ The array keys (``default ``, ``test ``, etc.) of this prototype option are
758
+ validation-free and can be any valid key you want to differentiate the connections.
759
+
760
+ .. note ::
761
+
762
+ A prototype option can only be defined inside a nested option and
763
+ during its resolution it will expect an array of array.
764
+
765
+ .. versionadded :: 5.3
766
+
767
+ The ``setPrototype() `` method was introduced in Symfony 5.3.
768
+
720
769
Deprecating the Option
721
770
~~~~~~~~~~~~~~~~~~~~~~
722
771
0 commit comments