Fix activity date filter: validation of form using RollingDate

This commit is contained in:
Julien Fastré 2023-11-05 23:29:33 +01:00
parent 68f56671a4
commit 6280453523
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 5 additions and 44 deletions

View File

@ -0,0 +1,5 @@
kind: Fixed
body: Fix filter "activity by date"
time: 2023-11-05T23:26:08.685518729+01:00
custom:
Issue: "184"

View File

@ -13,16 +13,12 @@ namespace Chill\ActivityBundle\Export\Filter;
use Chill\ActivityBundle\Export\Declarations;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\Export\FilterType;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
class ActivityDateFilter implements FilterInterface
@ -74,46 +70,6 @@ class ActivityDateFilter implements FilterInterface
->add('date_to', PickRollingDateType::class, [
'label' => 'Activities before this date',
]);
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
/** @var \Symfony\Component\Form\FormInterface $filterForm */
$filterForm = $event->getForm()->getParent();
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
if (true === $enabled) {
// if the filter is enabled, add some validation
$form = $event->getForm();
$date_from = $form->get('date_from')->getData();
$date_to = $form->get('date_to')->getData();
// check that fields are not empty
if (null === $date_from) {
$form->get('date_from')->addError(new FormError(
$this->translator->trans('This field '
.'should not be empty')
));
}
if (null === $date_to) {
$form->get('date_to')->addError(new FormError(
$this->translator->trans('This field '
.'should not be empty')
));
}
// check that date_from is before date_to
if (
(null !== $date_from && null !== $date_to)
&& $date_from >= $date_to
) {
$form->get('date_to')->addError(new FormError(
$this->translator->trans('This date should be after '
.'the date given in "Implied in an activity after '
.'this date" field')
));
}
}
});
}
public function getFormDefaultData(): array