feat: unique email addresses of participants unless child, cleanup
addresses #869axbn21
This commit is contained in:
@@ -195,4 +195,44 @@ class BookingDto
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
#[Assert\Callback(groups: ['booking_create_step_2', 'booking_edit'])]
|
||||
public function validateEmailUniqueness(ExecutionContextInterface $context): void
|
||||
{
|
||||
// Build map of email addresses to participant indices (adults only)
|
||||
$emailMap = [];
|
||||
|
||||
foreach ($this->participants as $index => $participant) {
|
||||
// Skip children (they can share email addresses)
|
||||
if (true === $participant->isChild()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip null or empty emails (handled by other validators)
|
||||
if (null === $participant->email || '' === trim($participant->email)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Normalize email for case-insensitive comparison
|
||||
$normalizedEmail = strtolower(trim($participant->email));
|
||||
|
||||
// Add participant index to the email map
|
||||
if (false === isset($emailMap[$normalizedEmail])) {
|
||||
$emailMap[$normalizedEmail] = [];
|
||||
}
|
||||
$emailMap[$normalizedEmail][] = $index;
|
||||
}
|
||||
|
||||
// Add validation errors for duplicate emails
|
||||
foreach ($emailMap as $email => $indices) {
|
||||
// Only add violations if email is used by multiple adult participants
|
||||
if (1 < count($indices)) {
|
||||
foreach ($indices as $index) {
|
||||
$context->buildViolation('Diese E-Mail Adresse wird bereits von einem anderen erwachsenen Teilnehmer verwendet')
|
||||
->atPath(sprintf('participants[%d].email', $index))
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,4 +224,26 @@ class ParticipantDto
|
||||
'message' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the participant is a child based on age at current date.
|
||||
*
|
||||
* A child is defined as someone under the specified age threshold (default: 16 years).
|
||||
* This classification is used for email uniqueness validation (children can share emails with adults).
|
||||
*
|
||||
* @param int $ageThreshold The age threshold for child classification (default: 16)
|
||||
*
|
||||
* @return bool True if participant is under the age threshold, false otherwise or if age unknown
|
||||
*/
|
||||
public function isChild(int $ageThreshold = 16): bool
|
||||
{
|
||||
$age = $this->getAge();
|
||||
|
||||
// Treat unknown age as adult for safety (requires email uniqueness)
|
||||
if (null === $age) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $ageThreshold > $age;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user