From b038d9a41f3b7f25a2a585b53d9640bfe80ec902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 27 Aug 2025 13:46:00 +0200 Subject: [PATCH] wip: age based filtering of options --- config/services.yaml | 1 + .../ParticipantDateOfBirthFieldHandler.php | 110 ++++++++++++++++++ templates/booking/create_step_2.html.twig | 8 +- 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/Form/Service/ParticipantDateOfBirthFieldHandler.php diff --git a/config/services.yaml b/config/services.yaml index b803881..370340d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -72,6 +72,7 @@ services: arguments: $handlers: # Simple handlers (no dependencies) - use class names + - 'App\Form\Service\ParticipantDateOfBirthFieldHandler' - 'App\Form\Service\ParticipantAssignedRoomFieldHandler' # Complex handlers (with dependencies) would use service references like: # - '@participant.complex.handler' diff --git a/src/Form/Service/ParticipantDateOfBirthFieldHandler.php b/src/Form/Service/ParticipantDateOfBirthFieldHandler.php new file mode 100644 index 0000000..c1d0541 --- /dev/null +++ b/src/Form/Service/ParticipantDateOfBirthFieldHandler.php @@ -0,0 +1,110 @@ + $submittedData The submitted participant form data + * @param BookingDtoInterface $bookingDto The booking DTO to update (create or edit) + * @param int $participantIndex The index of the participant being processed + */ + public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void + { + // Safely get the participant object, returning early if not found + $participant = $this->getParticipant($bookingDto, $participantIndex); + + if (null === $participant) { + return; + } + + // Extract the date of birth from form data (defaults to null if not present) + $dateOfBirth = $this->getFieldValue($submittedData, $this->getFieldName()); + + // Convert form date to DateTimeImmutable, handling empty selections as null + $participant->dateOfBirth = $this->normalizeDateValue($dateOfBirth); + } + + /** + * Normalizes a date value from form submission to DateTimeImmutable or null. + * + * This method handles the conversion of form date values to proper DateTimeImmutable + * objects. It gracefully handles empty strings, null values, and invalid date formats. + * + * @param mixed $value The raw value from form submission + * + * @return \DateTimeImmutable|null The normalized date or null if empty/invalid + */ + private function normalizeDateValue(mixed $value): ?\DateTimeImmutable + { + // Handle null or empty string (no date selected) + if (null === $value || '' === trim((string) $value)) { + return null; + } + + // If already a DateTimeInterface, convert to DateTimeImmutable + if ($value instanceof \DateTimeInterface) { + return \DateTimeImmutable::createFromInterface($value); + } + + // Try to parse string date + if (is_string($value)) { + try { + return new \DateTimeImmutable($value); + } catch (\Exception $e) { + // Invalid date format, return null + return null; + } + } + + // Unsupported type, return null + return null; + } +} \ No newline at end of file diff --git a/templates/booking/create_step_2.html.twig b/templates/booking/create_step_2.html.twig index e80abbc..3b15a49 100644 --- a/templates/booking/create_step_2.html.twig +++ b/templates/booking/create_step_2.html.twig @@ -27,7 +27,13 @@
{{ form_row(participant.firstName) }} {{ form_row(participant.lastName) }} - {{ form_row(participant.dateOfBirth) }} + {# hx-swap="none" tells HTMX not to do a normal swap, as OOB will handle it #} + {{ form_row(participant.dateOfBirth, { + 'attr': { + 'hx-post': path('app_booking_create_step_2_refresh'), + 'hx-swap': 'none' + } + }) }} {{ form_row(participant.gender) }} {{ form_row(participant.nationality) }}