Skip to content
This repository was archived by the owner on Mar 6, 2025. It is now read-only.

Commit c1c2178

Browse files
committed
Fix add participation validation
1 parent 157579f commit c1c2178

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/Entity/Participation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
/**
2020
* @ORM\Entity()
21+
* @CustomAssert\NoParticipationDuplicate()
2122
*/
2223
class Participation
2324
{
@@ -45,7 +46,6 @@ class Participation
4546
* @ORM\ManyToOne(targetEntity="App\Entity\Conference", inversedBy="participations", cascade={"persist"})
4647
* @ORM\JoinColumn(nullable=false)
4748
*
48-
* @CustomAssert\NoParticipationDuplicate()
4949
* @CustomAssert\NotEndedConference()
5050
*/
5151
private Conference $conference;

src/Validator/Constraints/NoParticipationDuplicate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@
1717
class NoParticipationDuplicate extends Constraint
1818
{
1919
public string $message = 'A participation at this conference is already registered for {{ user_name }}.';
20+
21+
public function getTargets()
22+
{
23+
return self::CLASS_CONSTRAINT;
24+
}
2025
}

src/Validator/Constraints/NoParticipationDuplicateValidator.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace App\Validator\Constraints;
1313

1414
use App\Entity\Conference;
15+
use App\Entity\Participation;
1516
use App\Entity\User;
1617
use App\Repository\ParticipationRepository;
1718
use Symfony\Component\Security\Core\Security;
@@ -22,36 +23,33 @@
2223
class NoParticipationDuplicateValidator extends ConstraintValidator
2324
{
2425
public function __construct(
25-
private Security $security,
2626
private ParticipationRepository $participationRepository,
2727
) {
2828
}
2929

3030
/**
31-
* @param Conference|null $conference
31+
* @param Participation|null $participation
3232
* @param NoParticipationDuplicate $constraint
3333
*/
34-
public function validate($conference, Constraint $constraint): void
34+
public function validate($participation, Constraint $constraint): void
3535
{
36-
if (!$conference) {
36+
if (!$participation) {
3737
return;
3838
}
3939

40-
if (!$conference instanceof Conference) {
41-
throw new UnexpectedValueException($conference, Conference::class);
40+
if (!$participation instanceof Participation) {
41+
throw new UnexpectedValueException($participation, Participation::class);
4242
}
4343

44-
/** @var User $user */
45-
$user = $this->security->getUser();
4644
$existingParticipation = $this->participationRepository->findOneBy([
47-
'participant' => $user,
48-
'conference' => $conference,
45+
'participant' => $participation->getParticipant(),
46+
'conference' => $participation->getConference(),
4947
]);
5048

5149
if ($existingParticipation) {
5250
$this->context
5351
->buildViolation($constraint->message)
54-
->setParameter('{{ user_name }}', $user->getName())
52+
->setParameter('{{ user_name }}', $participation->getParticipant()->getName())
5553
->addViolation()
5654
;
5755
}

0 commit comments

Comments
 (0)