From 78a3dfd65e7b3084f4cef67d4f8ae100a2eb7c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 8 Apr 2024 14:52:40 +0200 Subject: [PATCH] Add validation for household metadata A new validation group 'household_metadata' has been added to the Household form. The 'waitingForBirth' field now has a type check, and for the 'waitingForBirthDate' field, an expression is added to ensure it's set if 'waitingForBirth' is true. Simultaneously, error handling for form submission has been enriched in the HouseholdController to display specific error messages. --- .../ChillPersonBundle/Controller/HouseholdController.php | 8 +++++++- .../ChillPersonBundle/Entity/Household/Household.php | 2 ++ src/Bundle/ChillPersonBundle/Form/HouseholdType.php | 1 + .../Tests/Controller/HouseholdControllerTest.php | 4 +--- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdController.php index 83a195c5b..d63c5aea9 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdController.php @@ -220,6 +220,12 @@ class HouseholdController extends AbstractController 'household_id' => $household->getId(), ]); } + if ($form->isSubmitted() && !$form->isValid()) { + $this->addFlash('error', $this->translator->trans('This form contains errors')); + foreach ($form->getErrors() as $error) { + $this->addFlash('error', $error->getMessage()); + } + } return $this->render('@ChillPerson/Household/edit_member_metadata.html.twig', [ 'household' => $household, @@ -275,7 +281,7 @@ class HouseholdController extends AbstractController [ 'household' => $household, 'positions' => $positions, - 'form' => null !== $form ? $form->createView() : null, + 'form' => $form?->createView(), ] ); } diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index ea68f51ec..48494e034 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -68,10 +68,12 @@ class Household #[Serializer\Groups(['docgen:read'])] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, name: 'waiting_for_birth', options: ['default' => false])] + #[Assert\Type('boolean', groups: ['household_metadata'])] private bool $waitingForBirth = false; #[Serializer\Groups(['docgen:read'])] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATE_IMMUTABLE, name: 'waiting_for_birth_date', nullable: true, options: ['default' => null])] + #[Assert\Expression('this.getWaitingForBirth() == false or value != null', message: 'The waiting for birth date must be set', groups: ['household_metadata'])] private ?\DateTimeImmutable $waitingForBirthDate = null; public function __construct() diff --git a/src/Bundle/ChillPersonBundle/Form/HouseholdType.php b/src/Bundle/ChillPersonBundle/Form/HouseholdType.php index 4a949bb92..ae9bcb45f 100644 --- a/src/Bundle/ChillPersonBundle/Form/HouseholdType.php +++ b/src/Bundle/ChillPersonBundle/Form/HouseholdType.php @@ -43,6 +43,7 @@ class HouseholdType extends AbstractType { $resolver->setDefaults([ 'data_class' => Household::class, + 'validation_groups' => ['household_metadata'], ]); } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdControllerTest.php index 8aa3466c1..1012480b2 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdControllerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdControllerTest.php @@ -115,11 +115,9 @@ final class HouseholdControllerTest extends WebTestCase $form['household[commentMembers][comment]'] = 'This is a text **generated** by automatic tests'; $form['household[waitingForBirth]']->tick(); $form['household[waitingForBirthDate]'] = (new \DateTime('today')) - ->add(new \DateInterval('P1M'))->format('Y-m-d'); + ->add(new \DateInterval('P10M'))->format('Y-m-d'); - $this->client->followRedirects(false); $this->client->submit($form); - $response = $this->client->getResponse(); self::assertEquals(302, $response->getStatusCode());