feat: improved dynamic validation

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 5275e6c732
commit 5c5f164aa2
11 changed files with 143 additions and 292 deletions
+31 -3
View File
@@ -138,7 +138,7 @@ class ParticipantCardDataService
*
* @return array{name: string, email: string, roomName: string, price: string, isCanceled: bool, isValid: bool, errorMessages: array<string>}
*/
public function getCardDataWithValidation(BookingDto $bookingDto, int $index, array $validationGroups): array
public function getCardDataWithValidation(BookingDto $bookingDto, int $index): array
{
$participant = $bookingDto->participants[$index] ?? null;
@@ -155,6 +155,9 @@ class ParticipantCardDataService
bookingContext: $bookingDto,
);
// Determine validation groups based on mode and mutability
$validationGroups = $this->determineValidationGroups($bookingDto);
// Validate the wrapper DTO
$violations = $this->validator->validate($wrapper, null, $validationGroups);
@@ -177,14 +180,39 @@ class ParticipantCardDataService
*
* @return array<int, array{name: string, email: string, roomName: string, price: string, isCanceled: bool, isValid: bool, errorMessages: array<string>}>
*/
public function getAllCardsDataWithValidation(BookingDto $bookingDto, array $validationGroups): array
public function getAllCardsDataWithValidation(BookingDto $bookingDto): array
{
$cardsData = [];
foreach ($bookingDto->participants as $index => $participant) {
$cardsData[$index] = $this->getCardDataWithValidation($bookingDto, $index, $validationGroups);
$cardsData[$index] = $this->getCardDataWithValidation($bookingDto, $index);
}
return $cardsData;
}
/**
* Determines validation groups based on booking mode and applicant mutability.
*
* @return array<string> Validation groups to apply
*/
private function determineValidationGroups(BookingDto $bookingDto): array
{
$groups = [];
// Add mode-specific group
if (BookingDto::MODE_CREATE === $bookingDto->getMode()) {
$groups[] = 'booking_create';
$groups[] = 'strict_required';
} else {
$groups[] = 'booking_edit';
// In edit mode, add strict_required only if applicant is immutable
if (false === ($bookingDto->participants[0]?->mutable ?? true)) {
$groups[] = 'strict_required';
}
}
return $groups;
}
}