Validate single submission to call per project
This commit is contained in:
@@ -4,8 +4,10 @@ namespace App\Dto\Matchfunding;
|
||||
|
||||
use App\ApiResource\Matchfunding\MatchCallApiResource;
|
||||
use App\ApiResource\Project\ProjectApiResource;
|
||||
use App\Validator\SingleProjectSubmissionPerCall;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[SingleProjectSubmissionPerCall()]
|
||||
class MatchCallSubmissionCreationDto
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute]
|
||||
final class SingleProjectSubmissionPerCall extends Constraint
|
||||
{
|
||||
public string $message = 'There is an existing MatchCallSubmission for that Project to that MatchCall.';
|
||||
|
||||
public function __construct(
|
||||
public string $mode = 'strict',
|
||||
?array $groups = null,
|
||||
mixed $payload = null
|
||||
) {
|
||||
parent::__construct([], $groups, $payload);
|
||||
}
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Validator;
|
||||
|
||||
use App\Dto\Matchfunding\MatchCallSubmissionCreationDto;
|
||||
use App\Repository\Matchfunding\MatchCallSubmissionRepository;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
final class SingleProjectSubmissionPerCallValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private MatchCallSubmissionRepository $matchCallSubmissionRepository
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param MatchCallSubmissionCreationDto $value
|
||||
*/
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
$submission = $this->matchCallSubmissionRepository->findOneBy([
|
||||
'call' => $value->call->id,
|
||||
'project' => $value->project->id
|
||||
]);
|
||||
|
||||
if ($submission === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user