Skip to content

Commit 8db6dc3

Browse files
committed
minor #15325 [OptionsResolver] Documenting new setPrototype() method (yceruto)
This PR was merged into the 5.3-dev branch. Discussion ---------- [OptionsResolver] Documenting new setPrototype() method PR: symfony/symfony#39913 Fix: #15301 Commits ------- 04316b2 Documenting prototype options
2 parents a203286 + 04316b2 commit 8db6dc3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

components/options_resolver.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,55 @@ In same way, parent options can access to the nested options as normal arrays::
717717
The fact that an option is defined as nested means that you must pass
718718
an array of values to resolve it at runtime.
719719

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+
720769
Deprecating the Option
721770
~~~~~~~~~~~~~~~~~~~~~~
722771

0 commit comments

Comments
 (0)