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

Commit 910d252

Browse files
committed
Fix add participation validation
1 parent a711c0e commit 910d252

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/Entity/Participation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* @ORM\Entity()
12+
*
13+
* @CustomAssert\NoParticipationDuplicate()
1214
*/
1315
class Participation
1416
{
@@ -39,8 +41,6 @@ class Participation
3941
*
4042
* @ORM\JoinColumn(nullable=false)
4143
*
42-
* @CustomAssert\NoParticipationDuplicate()
43-
*
4444
* @CustomAssert\NotEndedConference()
4545
*/
4646
private Conference $conference;

src/Validator/Constraints/NoParticipationDuplicate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
class NoParticipationDuplicate extends Constraint
99
{
1010
public string $message = 'A participation at this conference is already registered for {{ user_name }}.';
11+
12+
public function getTargets()
13+
{
14+
return self::CLASS_CONSTRAINT;
15+
}
1116
}

src/Validator/Constraints/NoParticipationDuplicateValidator.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,42 @@
22

33
namespace App\Validator\Constraints;
44

5-
use App\Entity\Conference;
6-
use App\Entity\User;
5+
use App\Entity\Participation;
76
use App\Repository\ParticipationRepository;
8-
use Symfony\Component\Security\Core\Security;
97
use Symfony\Component\Validator\Constraint;
108
use Symfony\Component\Validator\ConstraintValidator;
119
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1210

1311
class NoParticipationDuplicateValidator extends ConstraintValidator
1412
{
1513
public function __construct(
16-
private Security $security,
1714
private ParticipationRepository $participationRepository,
1815
) {
1916
}
2017

2118
/**
22-
* @param Conference|null $conference
19+
* @param Participation|null $participation
2320
* @param NoParticipationDuplicate $constraint
2421
*/
25-
public function validate($conference, Constraint $constraint): void
22+
public function validate($participation, Constraint $constraint): void
2623
{
27-
if (!$conference) {
24+
if (!$participation) {
2825
return;
2926
}
3027

31-
if (!$conference instanceof Conference) {
32-
throw new UnexpectedValueException($conference, Conference::class);
28+
if (!$participation instanceof Participation) {
29+
throw new UnexpectedValueException($participation, Participation::class);
3330
}
3431

35-
/** @var User $user */
36-
$user = $this->security->getUser();
3732
$existingParticipation = $this->participationRepository->findOneBy([
38-
'participant' => $user,
39-
'conference' => $conference,
33+
'participant' => $participation->getParticipant(),
34+
'conference' => $participation->getConference(),
4035
]);
4136

4237
if ($existingParticipation) {
4338
$this->context
4439
->buildViolation($constraint->message)
45-
->setParameter('{{ user_name }}', $user->getName())
40+
->setParameter('{{ user_name }}', $participation->getParticipant()->getName())
4641
->addViolation()
4742
;
4843
}

0 commit comments

Comments
 (0)