#4 ValidInvite constraint implemented.
This commit is contained in:
parent
16f8d1ff32
commit
01827680a1
|
@ -3,6 +3,7 @@
|
||||||
namespace App\FormRequest;
|
namespace App\FormRequest;
|
||||||
|
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
use App\Validator\Constraints as AppAssert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo implement UniqueEntity constraint for DTO and use it here
|
* @todo implement UniqueEntity constraint for DTO and use it here
|
||||||
|
@ -37,6 +38,7 @@ class CreateUserRequest
|
||||||
*
|
*
|
||||||
* @Assert\NotBlank()
|
* @Assert\NotBlank()
|
||||||
* @Assert\Length(min="32", max="32")
|
* @Assert\Length(min="32", max="32")
|
||||||
|
* @AppAssert\ValidInvite()
|
||||||
*/
|
*/
|
||||||
public $inviteCode;
|
public $inviteCode;
|
||||||
|
|
||||||
|
|
19
src/Validator/Constraints/ValidInvite.php
Normal file
19
src/Validator/Constraints/ValidInvite.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
class ValidInvite extends Constraint
|
||||||
|
{
|
||||||
|
public $notFoundMessage = 'Invite {{ code }} not found.';
|
||||||
|
public $usedMessage = 'Invite {{ code }} is used.';
|
||||||
|
|
||||||
|
public function validatedBy(): string
|
||||||
|
{
|
||||||
|
return get_class($this).'Validator';
|
||||||
|
}
|
||||||
|
}
|
42
src/Validator/Constraints/ValidInviteValidator.php
Normal file
42
src/Validator/Constraints/ValidInviteValidator.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\Entity\Invite;
|
||||||
|
use App\Repository\InviteRepository;
|
||||||
|
use Symfony\Component\Validator\{Constraint, ConstraintValidator};
|
||||||
|
|
||||||
|
class ValidInviteValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/** @var InviteRepository */
|
||||||
|
private $inviteRepo;
|
||||||
|
|
||||||
|
public function __construct(InviteRepository $inviteRepo)
|
||||||
|
{
|
||||||
|
$this->inviteRepo = $inviteRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @param ValidInvite $constraint
|
||||||
|
*/
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
/** @var Invite $invite */
|
||||||
|
if (null === $invite = $this->inviteRepo->findOneBy(['code' => $value])) {
|
||||||
|
$this->context->buildViolation($constraint->notFoundMessage)
|
||||||
|
->setParameter('{{ code }}', $value)
|
||||||
|
->addViolation()
|
||||||
|
;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $invite->getUsedBy()) {
|
||||||
|
$this->context->buildViolation($constraint->usedMessage)
|
||||||
|
->setParameter('{{ code }}', $value)
|
||||||
|
->addViolation()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue