From 4e3c93cdd3b0e244491f8456f58ed85463af1cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 7 Nov 2022 17:56:30 +0100 Subject: [PATCH] DX: Add validation to PickRolingDateType It is not possible to pick a fixed date when not setting a fixed date --- .../Form/Type/PickRollingDateType.php | 17 +++++++++++++++++ .../Service/RollingDate/RollingDate.php | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php b/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php index a54dc778b..a3013f792 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php @@ -17,6 +17,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Validator\Constraints\Callback; +use Symfony\Component\Validator\Context\ExecutionContextInterface; class PickRollingDateType extends AbstractType { @@ -45,6 +47,21 @@ class PickRollingDateType extends AbstractType $resolver->setDefaults([ 'class' => RollingDate::class, 'empty_data' => new RollingDate(RollingDate::T_TODAY), + 'constraints' => [ + new Callback([$this, 'validate']) + ] ]); } + + public function validate($data, ExecutionContextInterface $context, $payload): void + { + /** @var RollingDate $data */ + if (RollingDate::T_FIXED_DATE === $data->getRoll() && null === $data->getFixedDate()) { + $context + ->buildViolation('rolling_date.When fixed date is selected, you must provide a date') + ->atPath('roll') + ->addViolation() + ; + } + } } diff --git a/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php index 7294e6816..e8427c62f 100644 --- a/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php +++ b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php @@ -84,12 +84,12 @@ class RollingDate $this->fixedDate = $fixedDate; } - public function getFixedDate(): DateTimeImmutable + public function getFixedDate(): ?DateTimeImmutable { return $this->fixedDate; } - public function getPivotDate(): ?DateTimeImmutable + public function getPivotDate(): DateTimeImmutable { return $this->pivotDate; }