|
2 | 2 |
|
3 | 3 | namespace App\Form; |
4 | 4 |
|
| 5 | +use App\Enum\Food; |
| 6 | +use App\Enum\Meal; |
| 7 | +use App\Enum\PizzaSize; |
5 | 8 | use App\Model\MealPlan; |
6 | 9 | use Symfony\Component\Form\AbstractType; |
7 | | -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 10 | +use Symfony\Component\Form\Extension\Core\Type\EnumType; |
8 | 11 | use Symfony\Component\Form\FormBuilderInterface; |
9 | 12 | use Symfony\Component\Form\FormEvent; |
10 | 13 | use Symfony\Component\Form\FormEvents; |
| 14 | +use Symfony\Component\Form\FormFactoryInterface; |
11 | 15 | use Symfony\Component\Form\FormInterface; |
12 | 16 | use Symfony\Component\OptionsResolver\OptionsResolver; |
13 | 17 |
|
14 | 18 | class MealPlannerForm extends AbstractType |
15 | 19 | { |
16 | | - public const MEAL_BREAKFAST = 'breakfast'; |
17 | | - public const MEAL_SECOND_BREAKFAST = 'second breakfast'; |
18 | | - public const MEAL_ELEVENSES = 'elevenses'; |
19 | | - public const MEAL_LUNCH = 'lunch'; |
20 | | - public const MEAL_DINNER = 'dinner'; |
| 20 | + private FormFactoryInterface $factory; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var array<string, mixed> |
| 24 | + */ |
| 25 | + private $dependencies = []; |
21 | 26 |
|
22 | 27 | public function buildForm(FormBuilderInterface $builder, array $options) |
23 | 28 | { |
24 | | - $choices = [ |
25 | | - 'Breakfast' => self::MEAL_BREAKFAST, |
26 | | - 'Second Breakfast' => self::MEAL_SECOND_BREAKFAST, |
27 | | - 'Elevenses' => self::MEAL_ELEVENSES, |
28 | | - 'Lunch' => self::MEAL_LUNCH, |
29 | | - 'Dinner' => self::MEAL_DINNER, |
30 | | - ]; |
31 | | - $builder->add('meal', ChoiceType::class, [ |
32 | | - 'choices' => $choices, |
| 29 | + $this->factory = $builder->getFormFactory(); |
| 30 | + |
| 31 | + $builder->add('meal', EnumType::class, [ |
| 32 | + 'class' => Meal::class, |
| 33 | + 'choice_label' => fn (Meal $meal): string => $meal->getReadable(), |
33 | 34 | 'placeholder' => 'Which meal is it?', |
34 | 35 | 'autocomplete' => true, |
35 | 36 | ]); |
36 | 37 |
|
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 | | - |
44 | | - $meal = $data?->getMeal(); |
45 | | - $this->addFoodField($event->getForm(), $meal); |
46 | | - } |
47 | | - ); |
| 38 | + $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']); |
| 39 | + $builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']); |
48 | 40 |
|
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 | | - ); |
| 41 | + $builder->get('meal')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'storeDependencies']); |
| 42 | + $builder->get('meal')->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmitMeal']); |
61 | 43 | } |
62 | 44 |
|
63 | | - public function configureOptions(OptionsResolver $resolver) |
| 45 | + public function configureOptions(OptionsResolver $resolver): void |
64 | 46 | { |
65 | 47 | $resolver->setDefaults(['data_class' => MealPlan::class]); |
66 | 48 | } |
67 | 49 |
|
68 | | - private function getAvailableFoodChoices(string $meal): array |
| 50 | + public function onPreSetData(FormEvent $event): void |
69 | 51 | { |
70 | | - $foods = match ($meal) { |
71 | | - self::MEAL_BREAKFAST => ['Eggs 🍳', 'Bacon 🥓', 'Strawberries 🍓', 'Croissant 🥐'], |
72 | | - self::MEAL_SECOND_BREAKFAST => ['Bagel 🥯', 'Kiwi 🥝', 'Avocado 🥑', 'Waffles 🧇'], |
73 | | - self::MEAL_ELEVENSES => ['Pancakes 🥞', 'Salad 🥙', 'Tea ☕️'], |
74 | | - self::MEAL_LUNCH => ['Sandwich 🥪', 'Cheese 🧀', 'Sushi 🍱'], |
75 | | - self::MEAL_DINNER => ['Pizza 🍕', 'A Pint 🍺', 'Pasta 🍝'], |
76 | | - }; |
| 52 | + // the object tied to your form |
| 53 | + /** @var ?MealPlan $data */ |
| 54 | + $data = $event->getData(); |
| 55 | + |
| 56 | + $this->addFoodField($event->getForm(), $data?->getMeal()); |
| 57 | + $this->addPizzaSizeField($event->getForm(), $data?->getPizzaSize()); |
| 58 | + } |
77 | 59 |
|
78 | | - $foods = array_combine($foods, $foods); |
| 60 | + public function onPostSubmit(FormEvent $event): void |
| 61 | + { |
| 62 | + $this->dependencies = []; |
| 63 | + } |
79 | 64 |
|
80 | | - return $foods; |
| 65 | + public function storeDependencies(FormEvent $event): void |
| 66 | + { |
| 67 | + $this->dependencies[$event->getForm()->getName()] = $event->getForm()->getData(); |
81 | 68 | } |
82 | 69 |
|
83 | | - public function addFoodField(FormInterface $form, ?string $meal) |
| 70 | + public function onPostSubmitMeal(FormEvent $event): void |
84 | 71 | { |
85 | | - $foodChoices = null === $meal ? [] : $this->getAvailableFoodChoices($meal); |
86 | | - |
87 | | - $form->add('mainFood', ChoiceType::class, [ |
88 | | - 'placeholder' => null === $meal ? 'Select a meal first' : sprintf('What\'s for %s?', $meal), |
89 | | - 'choices' => $foodChoices, |
90 | | - 'disabled' => null === $meal, |
91 | | - // silence real-time "invalid" message when switching "meals" |
92 | | - 'invalid_message' => false, |
| 72 | + $this->addFoodField( |
| 73 | + $event->getForm()->getParent(), |
| 74 | + $this->dependencies['meal'], |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function onPostSubmitFood(FormEvent $event): void |
| 79 | + { |
| 80 | + $this->addPizzaSizeField( |
| 81 | + $event->getForm()->getParent(), |
| 82 | + $this->dependencies['mainFood'], |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + public function addFoodField(FormInterface $form, ?Meal $meal): void |
| 87 | + { |
| 88 | + $mainFood = $this->factory |
| 89 | + ->createNamedBuilder('mainFood', EnumType::class, $meal, [ |
| 90 | + 'class' => Food::class, |
| 91 | + 'placeholder' => null === $meal ? 'Select a meal first' : sprintf('What\'s for %s?', $meal->getReadable()), |
| 92 | + 'choices' => $meal?->getFoodChoices(), |
| 93 | + 'choice_label' => fn (Food $food): string => $food->getReadable(), |
| 94 | + 'disabled' => null === $meal, |
| 95 | + // silence real-time "invalid" message when switching "meals" |
| 96 | + 'invalid_message' => false, |
| 97 | + 'autocomplete' => true, |
| 98 | + 'auto_initialize' => false, |
| 99 | + ]) |
| 100 | + ->addEventListener(FormEvents::POST_SUBMIT, [$this, 'storeDependencies']) |
| 101 | + ->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmitFood']); |
| 102 | + |
| 103 | + $form->add($mainFood->getForm()); |
| 104 | + } |
| 105 | + |
| 106 | + public function addPizzaSizeField(FormInterface $form, ?Food $food): void |
| 107 | + { |
| 108 | + if (Food::Pizza !== $food) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + $form->add('pizzaSize', EnumType::class, [ |
| 113 | + 'class' => PizzaSize::class, |
| 114 | + 'placeholder' => 'What size pizza?', |
| 115 | + 'choice_label' => fn (PizzaSize $pizzaSize): string => $pizzaSize->getReadable(), |
| 116 | + 'required' => true, |
93 | 117 | 'autocomplete' => true, |
94 | 118 | ]); |
95 | 119 | } |
|
0 commit comments