From 1f5877460b4dc24a6716aa792bb6cd5bca7e4263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 23 Jul 2025 17:15:39 +0200 Subject: [PATCH] wip: refactor forms --- src/Controller/Booking/BookingCreateTrait.php | 7 +--- .../Booking/CreateStep2Controller.php | 4 +- src/Form/BookingCreateParticipantType.php | 20 +++------ src/Form/BookingCreateStep2Type.php | 11 ++--- .../Service/ParticipantFormConfigurator.php | 41 +++++++++++++++++++ 5 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 src/Form/Service/ParticipantFormConfigurator.php diff --git a/src/Controller/Booking/BookingCreateTrait.php b/src/Controller/Booking/BookingCreateTrait.php index 1ee5570..61f52a7 100644 --- a/src/Controller/Booking/BookingCreateTrait.php +++ b/src/Controller/Booking/BookingCreateTrait.php @@ -18,9 +18,6 @@ trait BookingCreateTrait { /** * Validates step access and redirects if necessary. - * - * @param BookingCreateDto $bookingCreateDto - * @param int $expectedStep */ private function validateStepAccess(BookingCreateDto $bookingCreateDto, int $expectedStep): void { @@ -34,8 +31,6 @@ trait BookingCreateTrait /** * Redirects to the current step based on the DTO's currentStep. - * - * @param BookingCreateDto $bookingCreateDto */ private function redirectToCurrentStep(BookingCreateDto $bookingCreateDto): RedirectResponse { @@ -53,4 +48,4 @@ trait BookingCreateTrait return $this->redirectToRoute($route, $routeParams); } -} \ No newline at end of file +} diff --git a/src/Controller/Booking/CreateStep2Controller.php b/src/Controller/Booking/CreateStep2Controller.php index 73b68ac..22265ea 100644 --- a/src/Controller/Booking/CreateStep2Controller.php +++ b/src/Controller/Booking/CreateStep2Controller.php @@ -27,7 +27,8 @@ class CreateStep2Controller extends AbstractController public function __construct( private readonly BookingService $bookingService, - ) { + ) + { } /** @@ -89,6 +90,7 @@ class CreateStep2Controller extends AbstractController ]); $form->handleRequest($request); + $this->bookingService->saveBookingCreateDto($request, $bookingCreateDto); $roomAssignmentCounts = $this->bookingService->getRoomAssignmentCounts($bookingCreateDto); diff --git a/src/Form/BookingCreateParticipantType.php b/src/Form/BookingCreateParticipantType.php index e7e238b..0446a11 100644 --- a/src/Form/BookingCreateParticipantType.php +++ b/src/Form/BookingCreateParticipantType.php @@ -5,7 +5,7 @@ namespace App\Form; use App\BusProNet\Form\CountryType; use App\Form\Model\BookingCreateDto; use App\Form\Model\ParticipantDto; -use App\Form\Service\ParticipantRoomChoiceLoaderFactory; +use App\Form\Service\ParticipantFormConfigurator; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\BirthdayType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; @@ -19,7 +19,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; class BookingCreateParticipantType extends AbstractType { public function __construct( - private readonly ParticipantRoomChoiceLoaderFactory $choiceLoaderFactory, + private readonly ParticipantFormConfigurator $formConfigurator, ) { } @@ -66,7 +66,7 @@ class BookingCreateParticipantType extends AbstractType 'clean_xss' => true, ]) ->add('bodyDimensions', BodyDimensionsType::class) - ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { + ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { /** @var ParticipantDto|null $participantData */ $participantData = $event->getData(); $form = $event->getForm(); @@ -83,19 +83,9 @@ class BookingCreateParticipantType extends AbstractType /** @var BookingCreateDto $bookingCreateDto */ $bookingCreateDto = $rootForm->getData(); - $allParticipants = $bookingCreateDto->participants; - $choiceLoader = $this->choiceLoaderFactory->create( - $allParticipants, - $options['selected_rooms'], - $participantData->index - ); - - $form->add('assignedRoomId', ChoiceType::class, [ - 'label' => 'Zimmer', - 'placeholder' => 'Bitte wählen', - 'choice_loader' => $choiceLoader, - ]); + $roomOptions = $this->formConfigurator->getRoomFieldOptions($bookingCreateDto, $participantData->index); + $form->add('assignedRoomId', ChoiceType::class, $roomOptions); }); } diff --git a/src/Form/BookingCreateStep2Type.php b/src/Form/BookingCreateStep2Type.php index 917cf4a..86d9495 100644 --- a/src/Form/BookingCreateStep2Type.php +++ b/src/Form/BookingCreateStep2Type.php @@ -31,7 +31,7 @@ class BookingCreateStep2Type extends AbstractType return; } - $this->addParticipantsField($event->getForm(), $data); + $this->addParticipantsField($event->getForm()); } /** @@ -59,26 +59,23 @@ class BookingCreateStep2Type extends AbstractType if (isset($participantData['assignedRoomId']) && isset($bookingDto->participants[$index])) { $roomId = $participantData['assignedRoomId']; // An unselected choice submits an empty string. - $bookingDto->participants[$index]->assignedRoomId = empty($roomId) ? null : (int) $roomId; + $bookingDto->participants[$index]->assignedRoomId = empty($roomId) ? null : (int)$roomId; } } // Now, rebuild the 'participants' field with the updated DTO. - $this->addParticipantsField($form, $bookingDto); + $this->addParticipantsField($form); } /** * Adds or replaces the 'participants' collection field on the form. */ - private function addParticipantsField(FormInterface $form, BookingCreateDto $data): void + private function addParticipantsField(FormInterface $form): void { $form->add('participants', CollectionType::class, [ 'entry_type' => BookingCreateParticipantType::class, 'allow_add' => false, 'allow_delete' => false, - 'entry_options' => [ - 'selected_rooms' => $data->getSelectedRooms(), - ], ]); } diff --git a/src/Form/Service/ParticipantFormConfigurator.php b/src/Form/Service/ParticipantFormConfigurator.php new file mode 100644 index 0000000..9c280be --- /dev/null +++ b/src/Form/Service/ParticipantFormConfigurator.php @@ -0,0 +1,41 @@ +roomChoiceLoaderFactory->create( + $bookingDto->participants, + $bookingDto->getSelectedRooms(), + $participantIndex + ); + + return [ + 'label' => 'Zimmer', + 'placeholder' => 'Bitte wählen', + 'choice_loader' => $choiceLoader, + ]; + } +}