Skip to content

Commit be80b06

Browse files
committed
refactor for readability
1 parent b1aba24 commit be80b06

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

ux.symfony.com/src/Form/MealPlannerForm.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class MealPlannerForm extends AbstractType
1919
public const MEAL_LUNCH = 'lunch';
2020
public const MEAL_DINNER = 'dinner';
2121

22+
/**
23+
* @var array<string, mixed>
24+
*/
25+
private $dependencies = [];
26+
2227
public function buildForm(FormBuilderInterface $builder, array $options)
2328
{
2429
$choices = [
@@ -28,43 +33,46 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2833
'Lunch' => self::MEAL_LUNCH,
2934
'Dinner' => self::MEAL_DINNER,
3035
];
36+
3137
$builder->add('meal', ChoiceType::class, [
3238
'choices' => $choices,
3339
'placeholder' => 'Which meal is it?',
3440
'autocomplete' => true,
3541
]);
3642

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']);
4344

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']);
6147
}
6248

63-
public function configureOptions(OptionsResolver $resolver)
49+
public function configureOptions(OptionsResolver $resolver): void
6450
{
6551
$resolver->setDefaults(['data_class' => MealPlan::class]);
6652
}
6753

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+
6876
private function getAvailableFoodChoices(string $meal): array
6977
{
7078
$foods = match ($meal) {
@@ -75,12 +83,10 @@ private function getAvailableFoodChoices(string $meal): array
7583
self::MEAL_DINNER => ['Pizza 🍕', 'A Pint 🍺', 'Pasta 🍝'],
7684
};
7785

78-
$foods = array_combine($foods, $foods);
79-
80-
return $foods;
86+
return array_combine($foods, $foods);
8187
}
8288

83-
public function addFoodField(FormInterface $form, ?string $meal)
89+
public function addFoodField(FormInterface $form, ?string $meal): void
8490
{
8591
$foodChoices = null === $meal ? [] : $this->getAvailableFoodChoices($meal);
8692

0 commit comments

Comments
 (0)