6
6
use Symfony \Component \Form \Exception \TransformationFailedException ;
7
7
use Symfony \Component \Form \Extension \Core \Type \FormType ;
8
8
use Symfony \Component \Form \Extension \Csrf \Type \FormTypeCsrfExtension ;
9
+ use Symfony \Component \Form \FormBuilderInterface ;
9
10
use Symfony \Component \Form \FormFactoryInterface ;
10
11
use Symfony \Component \Form \FormInterface ;
11
12
use Symfony \Component \Form \FormRegistryInterface ;
12
- use Symfony \Component \Form \Test \FormBuilderInterface ;
13
13
14
14
final class ConsoleFormWithDefaultValuesAndOptionsFactory implements ConsoleFormFactory
15
15
{
@@ -29,39 +29,72 @@ public function __construct(FormFactoryInterface $formFactory, FormRegistryInter
29
29
$ this ->formRegistry = $ formRegistry ;
30
30
}
31
31
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
+
32
47
public function create (string $ formType , InputInterface $ input , array $ options = []): FormInterface
33
48
{
34
49
$ options = $ this ->addDefaultOptions ($ options );
35
50
36
51
$ formBuilder = $ this ->formFactory ->createBuilder ($ formType , null , $ options );
37
52
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 ()) {
39
65
/** @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 ();
40
76
if (!$ input ->hasOption ($ name )) {
41
- continue ;
77
+ return ;
42
78
}
43
79
44
80
$ providedValue = $ input ->getOption ($ name );
45
81
if ($ providedValue === null ) {
46
- continue ;
82
+ return ;
47
83
}
48
84
49
85
$ value = $ providedValue ;
50
-
51
86
try {
52
- foreach ($ childBuilder ->getViewTransformers () as $ viewTransformer ) {
87
+ foreach ($ formBuilder ->getViewTransformers () as $ viewTransformer ) {
53
88
$ value = $ viewTransformer ->reverseTransform ($ value );
54
89
}
55
- foreach ($ childBuilder ->getModelTransformers () as $ modelTransformer ) {
90
+ foreach ($ formBuilder ->getModelTransformers () as $ modelTransformer ) {
56
91
$ value = $ modelTransformer ->reverseTransform ($ value );
57
92
}
58
93
} catch (TransformationFailedException ) {
59
94
}
60
95
61
- $ childBuilder ->setData ($ value );
96
+ $ formBuilder ->setData ($ value );
62
97
}
63
-
64
- return $ formBuilder ->getForm ();
65
98
}
66
99
67
100
private function addDefaultOptions (array $ options ): array
0 commit comments