From 9a95daa3db412543fbf323b6bb208943d4200a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Tue, 19 Sep 2023 16:32:14 +0200 Subject: [PATCH] WIP: Implement profile validation --- src/Controller/Teamer/ProfileController.php | 6 ++++ src/Entity/Embeddable/Address.php | 6 ++-- src/Entity/Embeddable/BankAccount.php | 8 ++--- src/Entity/Teamer.php | 14 ++++---- src/Validator/Constraints/BankAccount.php | 23 ++++++++++++ .../Constraints/BankAccountValidator.php | 36 +++++++++++++++++++ templates/teamer/profile.html.twig | 14 ++++++-- 7 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 src/Validator/Constraints/BankAccount.php create mode 100644 src/Validator/Constraints/BankAccountValidator.php diff --git a/src/Controller/Teamer/ProfileController.php b/src/Controller/Teamer/ProfileController.php index 13ea545..2581728 100644 --- a/src/Controller/Teamer/ProfileController.php +++ b/src/Controller/Teamer/ProfileController.php @@ -17,10 +17,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Attribute\IsGranted; +use Symfony\Component\Validator\Validator\ValidatorInterface; class ProfileController extends AbstractController { public function __construct( + private readonly ValidatorInterface $validator, private readonly EntityManagerInterface $entityManager, private readonly ApiClient $apiClient, private readonly UploadHandler $uploadHandler, @@ -48,6 +50,9 @@ class ProfileController extends AbstractController $this->updatePhoto($user, $uploadSession); } + // Validate teamer data to show missing data right away + $errors = $this->validator->validate($teamer); + $form = $this->createForm(TeamerProfileType::class, $teamer, ['upload_session' => $uploadSession]); $form->handleRequest($request); @@ -72,6 +77,7 @@ class ProfileController extends AbstractController return $this->render('teamer/profile.html.twig', [ 'form' => $form->createView(), 'teamer' => $teamer, + 'errors' => $errors, ]); } diff --git a/src/Entity/Embeddable/Address.php b/src/Entity/Embeddable/Address.php index d96cf8f..320e88f 100644 --- a/src/Entity/Embeddable/Address.php +++ b/src/Entity/Embeddable/Address.php @@ -11,15 +11,15 @@ use Symfony\Component\Validator\Constraints as Assert; class Address { #[ORM\Column(type: 'string', nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deine Straße an')] protected ?string $street = null; #[ORM\Column(type: 'string', nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deine Postleitzahl an')] protected ?string $postCode = null; #[ORM\Column(type: 'string', nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deinen Ort an')] protected ?string $city = null; #[ORM\Column(type: 'string', nullable: true)] diff --git a/src/Entity/Embeddable/BankAccount.php b/src/Entity/Embeddable/BankAccount.php index 51eedea..25ab6b3 100644 --- a/src/Entity/Embeddable/BankAccount.php +++ b/src/Entity/Embeddable/BankAccount.php @@ -2,27 +2,25 @@ namespace App\Entity\Embeddable; +use App\Validator\Constraints as AppAssert; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; #[ORM\Embeddable] +#[AppAssert\BankAccount] class BankAccount { #[ORM\Column(nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] - #[Assert\Iban(message: 'Diese IBAN ist ungültig')] + #[Assert\Iban(message: 'Die IBAN ist ungültig')] private ?string $iban = null; #[ORM\Column(nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] private ?string $bic = null; #[ORM\Column(nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] private ?string $bank = null; #[ORM\Column(nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] private ?string $holder = null; public function getIban(bool $obfuscated = false): ?string diff --git a/src/Entity/Teamer.php b/src/Entity/Teamer.php index 6dd041c..84e44c1 100644 --- a/src/Entity/Teamer.php +++ b/src/Entity/Teamer.php @@ -32,19 +32,19 @@ class Teamer implements TimestampableEntityInterface private string $uuid; #[ORM\Column(length: 255)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deinen Vornamen an')] private ?string $firstName = null; #[ORM\Column(length: 255)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deinen Nachnamen an')] private ?string $lastName = null; #[ORM\Column(length: 1)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib dein Geschlecht an')] private ?string $gender = null; #[ORM\Column(type: Types::DATE_IMMUTABLE)] - #[Assert\NotNull(message: 'Bitte angeben')] + #[Assert\NotNull(message: 'Bitte gib dein Geburtsdatum an')] private ?\DateTimeImmutable $dateOfBirth = null; #[ORM\Column(length: 255, nullable: true)] @@ -54,7 +54,7 @@ class Teamer implements TimestampableEntityInterface private ?string $salutation = null; #[ORM\Column(length: 255, nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deine Nationalität an')] private ?string $nationality = null; #[ORM\Embedded(class: Address::class)] @@ -70,11 +70,11 @@ class Teamer implements TimestampableEntityInterface private ?BankAccount $bankAccount = null; #[ORM\Column(length: 255, nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deine SteuerID an')] private ?string $taxId = null; #[ORM\Column(length: 255, nullable: true)] - #[Assert\NotBlank(message: 'Bitte angeben')] + #[Assert\NotBlank(message: 'Bitte gib deine Krankenversicherung an')] private ?string $healthInsuranceCompany = null; #[ORM\Column(length: 32)] diff --git a/src/Validator/Constraints/BankAccount.php b/src/Validator/Constraints/BankAccount.php new file mode 100644 index 0000000..50c2e53 --- /dev/null +++ b/src/Validator/Constraints/BankAccount.php @@ -0,0 +1,23 @@ +message = $message ?? $this->message; + } + + public function getTargets(): string + { + return Constraint::CLASS_CONSTRAINT; + } +} \ No newline at end of file diff --git a/src/Validator/Constraints/BankAccountValidator.php b/src/Validator/Constraints/BankAccountValidator.php new file mode 100644 index 0000000..e1c9ebc --- /dev/null +++ b/src/Validator/Constraints/BankAccountValidator.php @@ -0,0 +1,36 @@ +getIban() + || null === $value->getBic() + || null === $value->getBank() + || null === $value->getHolder() + ; + + if ($isIncomplete) { + $this->context + ->buildViolation($constraint->message) + ->atPath('iban') + ->addViolation() + ; + } + } +} \ No newline at end of file diff --git a/templates/teamer/profile.html.twig b/templates/teamer/profile.html.twig index c9e4626..49a9691 100644 --- a/templates/teamer/profile.html.twig +++ b/templates/teamer/profile.html.twig @@ -7,7 +7,7 @@ {% for child in form.children %} {% for error in child.vars.errors %}
  • - {{ child.vars.label }}: {{error.message}} + {{error.message}}
  • {% endfor %} {%endfor%} @@ -42,7 +42,17 @@ {{ _self.formErrors(form.communication) }} {{ _self.formErrors(form.bankAccount) }} - {%endif%} + {% elseif errors|length > 0 %} +
    + +
    + {% endif %}

    Persönliche Daten