DX: Add validation to PickRolingDateType

It is not possible to pick a fixed date when not setting a fixed date
This commit is contained in:
Julien Fastré 2022-11-07 17:56:30 +01:00
parent c7f740d5d7
commit 4e3c93cdd3
2 changed files with 19 additions and 2 deletions

View File

@ -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()
;
}
}
}

View File

@ -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;
}