WIP: Implement profile validation

This commit is contained in:
Björn Fromme
2023-09-19 16:32:14 +02:00
parent dc364cf320
commit 9a95daa3db
7 changed files with 90 additions and 17 deletions
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class BankAccount extends Constraint
{
public string $message = 'Die Bankverbindung ist unvollständig';
public function __construct(string $message = null, array $groups = null, $payload = null)
{
parent::__construct([], $groups, $payload);
$this->message = $message ?? $this->message;
}
public function getTargets(): string
{
return Constraint::CLASS_CONSTRAINT;
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Validator\Constraints;
use App\Entity\Embeddable\BankAccount as BankAccountEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class BankAccountValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$value instanceof BankAccountEntity) {
throw new UnexpectedTypeException($value, BankAccountEntity::class);
}
if (!$constraint instanceof BankAccount) {
throw new UnexpectedTypeException($constraint, BankAccount::class);
}
$isIncomplete = null === $value->getIban()
|| null === $value->getBic()
|| null === $value->getBank()
|| null === $value->getHolder()
;
if ($isIncomplete) {
$this->context
->buildViolation($constraint->message)
->atPath('iban')
->addViolation()
;
}
}
}