66use Symfony \Component \Form \Exception \TransformationFailedException ;
77use Symfony \Component \Form \Extension \Core \Type \FormType ;
88use Symfony \Component \Form \Extension \Csrf \Type \FormTypeCsrfExtension ;
9+ use Symfony \Component \Form \FormBuilderInterface ;
910use Symfony \Component \Form \FormFactoryInterface ;
1011use Symfony \Component \Form \FormInterface ;
1112use Symfony \Component \Form \FormRegistryInterface ;
12- use Symfony \Component \Form \Test \FormBuilderInterface ;
1313
1414final class ConsoleFormWithDefaultValuesAndOptionsFactory implements ConsoleFormFactory
1515{
@@ -29,39 +29,72 @@ public function __construct(FormFactoryInterface $formFactory, FormRegistryInter
2929 $ this ->formRegistry = $ formRegistry ;
3030 }
3131
32+ public function createNamed (
33+ string $ name ,
34+ string $ formType ,
35+ InputInterface $ input ,
36+ array $ options = []
37+ ): FormInterface {
38+ $ options = $ this ->addDefaultOptions ($ options );
39+
40+ $ formBuilder = $ this ->formFactory ->createNamedBuilder ($ name , $ formType , null , $ options );
41+
42+ $ this ->createChild ($ formBuilder , $ input , $ options );
43+
44+ return $ formBuilder ->getForm ();
45+ }
46+
3247 public function create (string $ formType , InputInterface $ input , array $ options = []): FormInterface
3348 {
3449 $ options = $ this ->addDefaultOptions ($ options );
3550
3651 $ formBuilder = $ this ->formFactory ->createBuilder ($ formType , null , $ options );
3752
38- foreach ($ formBuilder as $ name => $ childBuilder ) {
53+ $ this ->createChild ($ formBuilder , $ input , $ options );
54+
55+ return $ formBuilder ->getForm ();
56+ }
57+
58+ protected function createChild (
59+ FormBuilderInterface $ formBuilder ,
60+ InputInterface $ input ,
61+ array $ options ,
62+ ?string $ name = null
63+ ): void {
64+ if ($ formBuilder ->getCompound ()) {
3965 /** @var FormBuilderInterface $childBuilder */
66+ foreach ($ formBuilder as $ childName => $ childBuilder ) {
67+ $ this ->createChild (
68+ $ childBuilder ,
69+ $ input ,
70+ $ options ,
71+ $ name === null ? $ childName : $ name . '[ ' . $ childName . '] '
72+ );
73+ }
74+ } else {
75+ $ name = $ name ?? $ formBuilder ->getName ();
4076 if (!$ input ->hasOption ($ name )) {
41- continue ;
77+ return ;
4278 }
4379
4480 $ providedValue = $ input ->getOption ($ name );
4581 if ($ providedValue === null ) {
46- continue ;
82+ return ;
4783 }
4884
4985 $ value = $ providedValue ;
50-
5186 try {
52- foreach ($ childBuilder ->getViewTransformers () as $ viewTransformer ) {
87+ foreach ($ formBuilder ->getViewTransformers () as $ viewTransformer ) {
5388 $ value = $ viewTransformer ->reverseTransform ($ value );
5489 }
55- foreach ($ childBuilder ->getModelTransformers () as $ modelTransformer ) {
90+ foreach ($ formBuilder ->getModelTransformers () as $ modelTransformer ) {
5691 $ value = $ modelTransformer ->reverseTransform ($ value );
5792 }
5893 } catch (TransformationFailedException ) {
5994 }
6095
61- $ childBuilder ->setData ($ value );
96+ $ formBuilder ->setData ($ value );
6297 }
63-
64- return $ formBuilder ->getForm ();
6598 }
6699
67100 private function addDefaultOptions (array $ options ): array
0 commit comments