wip: age based filtering of options
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
||||
|
||||
/**
|
||||
* Handles processing of the dateOfBirth field for booking participants.
|
||||
*
|
||||
* This handler manages date of birth processing for participants in the booking creation
|
||||
* and edit processes. It processes the dateOfBirth field from form submissions and updates
|
||||
* the participant DTO with the provided date. This is crucial for age-based field visibility
|
||||
* and service filtering during HTMX form updates.
|
||||
*
|
||||
* Field Processing:
|
||||
* - Extracts date of birth from submitted form data
|
||||
* - Converts form date strings to DateTimeImmutable objects
|
||||
* - Handles empty selections (converting them to null)
|
||||
* - Updates the participant's dateOfBirth property
|
||||
*
|
||||
* Dependencies: None (this is a base field that other handlers may depend on)
|
||||
*/
|
||||
class ParticipantDateOfBirthFieldHandler extends AbstractParticipantFieldHandler
|
||||
{
|
||||
/**
|
||||
* Returns the form field name this handler processes.
|
||||
*
|
||||
* This handler is responsible for the 'dateOfBirth' field, which contains
|
||||
* the birth date for each participant in the booking form.
|
||||
*
|
||||
* @return string The field name 'dateOfBirth'
|
||||
*/
|
||||
public function getFieldName(): string
|
||||
{
|
||||
return 'dateOfBirth';
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the dateOfBirth field for a specific participant.
|
||||
*
|
||||
* This method extracts the date of birth from the submitted form data and
|
||||
* updates the corresponding participant in the booking DTO. It handles the
|
||||
* conversion from form date strings to DateTimeImmutable objects and properly
|
||||
* handles empty selections.
|
||||
*
|
||||
* Processing steps:
|
||||
* 1. Safely retrieves the participant object from the DTO
|
||||
* 2. Extracts the dateOfBirth value from submitted data
|
||||
* 3. Normalizes the value (empty string → null, date string → DateTimeImmutable)
|
||||
* 4. Updates the participant's dateOfBirth property
|
||||
*
|
||||
* @param array<string, mixed> $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;
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,13 @@
|
||||
<div class="grid grid-cols-2 gap-4 pb-4">
|
||||
{{ 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) }}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user