From c1c21785d04d2332b91d07280d8f07d2a0e99d52 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 | 2 +- .../Constraints/NoParticipationDuplicate.php | 5 +++++ .../NoParticipationDuplicateValidator.php | 20 +++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Entity/Participation.php b/src/Entity/Participation.php index a1e41d05..7f4618b1 100644 --- a/src/Entity/Participation.php +++ b/src/Entity/Participation.php @@ -18,6 +18,7 @@ /** * @ORM\Entity() + * @CustomAssert\NoParticipationDuplicate() */ class Participation { @@ -45,7 +46,6 @@ class Participation * @ORM\ManyToOne(targetEntity="App\Entity\Conference", inversedBy="participations", cascade={"persist"}) * @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 7a5b107d..aa1b9886 100644 --- a/src/Validator/Constraints/NoParticipationDuplicate.php +++ b/src/Validator/Constraints/NoParticipationDuplicate.php @@ -17,4 +17,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 62ed02fc..3f53bd98 100644 --- a/src/Validator/Constraints/NoParticipationDuplicateValidator.php +++ b/src/Validator/Constraints/NoParticipationDuplicateValidator.php @@ -12,6 +12,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; @@ -22,36 +23,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() ; }