From 8bac329b33810378605cb02304c4fec572251d6e Mon Sep 17 00:00:00 2001 From: Mathieu Santostefano Date: Tue, 19 Apr 2022 14:08:01 +0200 Subject: [PATCH] Fix add participation validation --- src/Entity/Participation.php | 3 +-- .../Constraints/NoParticipationDuplicate.php | 5 +++++ .../NoParticipationDuplicateValidator.php | 20 +++++++++---------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Entity/Participation.php b/src/Entity/Participation.php index 925145ca..18708ec3 100644 --- a/src/Entity/Participation.php +++ b/src/Entity/Participation.php @@ -9,6 +9,7 @@ /** * @ORM\Entity() + * @CustomAssert\NoParticipationDuplicate() */ class Participation { @@ -39,8 +40,6 @@ class Participation * * @ORM\JoinColumn(nullable=false) * - * @CustomAssert\NoParticipationDuplicate() - * * @CustomAssert\NotEndedConference() */ private Conference $conference; diff --git a/src/Validator/Constraints/NoParticipationDuplicate.php b/src/Validator/Constraints/NoParticipationDuplicate.php index f8138015..e006d327 100644 --- a/src/Validator/Constraints/NoParticipationDuplicate.php +++ b/src/Validator/Constraints/NoParticipationDuplicate.php @@ -8,4 +8,9 @@ class NoParticipationDuplicate extends Constraint { public string $message = 'A participation at this conference is already registered for {{ user_name }}.'; + + public function getTargets() + { + return self::CLASS_CONSTRAINT; + } } diff --git a/src/Validator/Constraints/NoParticipationDuplicateValidator.php b/src/Validator/Constraints/NoParticipationDuplicateValidator.php index a175ea3e..7923ce31 100644 --- a/src/Validator/Constraints/NoParticipationDuplicateValidator.php +++ b/src/Validator/Constraints/NoParticipationDuplicateValidator.php @@ -3,6 +3,7 @@ namespace App\Validator\Constraints; use App\Entity\Conference; +use App\Entity\Participation; use App\Entity\User; use App\Repository\ParticipationRepository; use Symfony\Component\Security\Core\Security; @@ -13,36 +14,33 @@ class NoParticipationDuplicateValidator extends ConstraintValidator { public function __construct( - private Security $security, private ParticipationRepository $participationRepository, ) { } /** - * @param Conference|null $conference + * @param Participation|null $participation * @param NoParticipationDuplicate $constraint */ - public function validate($conference, Constraint $constraint): void + public function validate($participation, Constraint $constraint): void { - if (!$conference) { + if (!$participation) { return; } - if (!$conference instanceof Conference) { - throw new UnexpectedValueException($conference, Conference::class); + if (!$participation instanceof Participation) { + throw new UnexpectedValueException($participation, Participation::class); } - /** @var User $user */ - $user = $this->security->getUser(); $existingParticipation = $this->participationRepository->findOneBy([ - 'participant' => $user, - 'conference' => $conference, + 'participant' => $participation->getParticipant(), + 'conference' => $participation->getConference(), ]); if ($existingParticipation) { $this->context ->buildViolation($constraint->message) - ->setParameter('{{ user_name }}', $user->getName()) + ->setParameter('{{ user_name }}', $participation->getParticipant()->getName()) ->addViolation() ; }