@@ -19,6 +19,11 @@ class MealPlannerForm extends AbstractType
19
19
public const MEAL_LUNCH = 'lunch ' ;
20
20
public const MEAL_DINNER = 'dinner ' ;
21
21
22
+ /**
23
+ * @var array<string, mixed>
24
+ */
25
+ private $ dependencies = [];
26
+
22
27
public function buildForm (FormBuilderInterface $ builder , array $ options )
23
28
{
24
29
$ choices = [
@@ -28,43 +33,46 @@ public function buildForm(FormBuilderInterface $builder, array $options)
28
33
'Lunch ' => self ::MEAL_LUNCH ,
29
34
'Dinner ' => self ::MEAL_DINNER ,
30
35
];
36
+
31
37
$ builder ->add ('meal ' , ChoiceType::class, [
32
38
'choices ' => $ choices ,
33
39
'placeholder ' => 'Which meal is it? ' ,
34
40
'autocomplete ' => true ,
35
41
]);
36
42
37
- $ builder ->addEventListener (
38
- FormEvents::PRE_SET_DATA ,
39
- function (FormEvent $ event ) {
40
- // the object tied to your form
41
- /** @var ?MealPlan $data */
42
- $ data = $ event ->getData ();
43
+ $ builder ->addEventListener (FormEvents::PRE_SET_DATA , [$ this , 'onPreSetData ' ]);
43
44
44
- $ meal = $ data ?->getMeal();
45
- $ this ->addFoodField ($ event ->getForm (), $ meal );
46
- }
47
- );
48
-
49
- $ builder ->get ('meal ' )->addEventListener (
50
- FormEvents::POST_SUBMIT ,
51
- function (FormEvent $ event ) {
52
- // It's important here to fetch $event->getForm()->getData(), as
53
- // $event->getData() will get you the client data (that is, the ID)
54
- $ meal = $ event ->getForm ()->getData ();
55
-
56
- // since we've added the listener to the child, we'll have to pass on
57
- // the parent to the callback functions!
58
- $ this ->addFoodField ($ event ->getForm ()->getParent (), $ meal );
59
- }
60
- );
45
+ $ builder ->get ('meal ' )->addEventListener (FormEvents::POST_SUBMIT , [$ this , 'storeDependencies ' ]);
46
+ $ builder ->get ('meal ' )->addEventListener (FormEvents::POST_SUBMIT , [$ this , 'onPostSubmitMeal ' ]);
61
47
}
62
48
63
- public function configureOptions (OptionsResolver $ resolver )
49
+ public function configureOptions (OptionsResolver $ resolver ): void
64
50
{
65
51
$ resolver ->setDefaults (['data_class ' => MealPlan::class]);
66
52
}
67
53
54
+ public function onPreSetData (FormEvent $ event ): void
55
+ {
56
+ // the object tied to your form
57
+ /** @var ?MealPlan $data */
58
+ $ data = $ event ->getData ();
59
+
60
+ $ this ->addFoodField ($ event ->getForm (), $ data ?->getMeal());
61
+ }
62
+
63
+ public function storeDependencies (FormEvent $ event ): void
64
+ {
65
+ $ this ->dependencies [$ event ->getForm ()->getName ()] = $ event ->getForm ()->getData ();
66
+ }
67
+
68
+ public function onPostSubmitMeal (FormEvent $ event ): void
69
+ {
70
+ $ this ->addFoodField (
71
+ $ event ->getForm ()->getParent (),
72
+ $ this ->dependencies ['meal ' ],
73
+ );
74
+ }
75
+
68
76
private function getAvailableFoodChoices (string $ meal ): array
69
77
{
70
78
$ foods = match ($ meal ) {
@@ -75,12 +83,10 @@ private function getAvailableFoodChoices(string $meal): array
75
83
self ::MEAL_DINNER => ['Pizza 🍕 ' , 'A Pint 🍺 ' , 'Pasta 🍝 ' ],
76
84
};
77
85
78
- $ foods = array_combine ($ foods , $ foods );
79
-
80
- return $ foods ;
86
+ return array_combine ($ foods , $ foods );
81
87
}
82
88
83
- public function addFoodField (FormInterface $ form , ?string $ meal )
89
+ public function addFoodField (FormInterface $ form , ?string $ meal ): void
84
90
{
85
91
$ foodChoices = null === $ meal ? [] : $ this ->getAvailableFoodChoices ($ meal );
86
92
0 commit comments