diff --git a/Export/Aggregator/AgeAggregator.php b/Export/Aggregator/AgeAggregator.php index 778a841ca..bed8bfa67 100644 --- a/Export/Aggregator/AgeAggregator.php +++ b/Export/Aggregator/AgeAggregator.php @@ -22,13 +22,16 @@ namespace Chill\PersonBundle\Export\Aggregator; use Chill\MainBundle\Export\AggregatorInterface; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\Extension\Core\Type\DateType; +use Chill\MainBundle\Export\ExportElementValidatedInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * * * @author Julien Fastré */ -class AgeAggregator implements AggregatorInterface +class AgeAggregator implements AggregatorInterface, + ExportElementValidatedInterface { public function addRole() @@ -58,6 +61,14 @@ class AgeAggregator implements AggregatorInterface 'format' => 'dd-MM-yyyy' )); } + + public function validateForm($data, ExecutionContextInterface $context) + { + if ($data['date_age_calculation'] === null) { + $context->buildViolation("The date should not be empty") + ->addViolation(); + } + } public function getLabels($key, array $values, $data) { diff --git a/Export/Aggregator/CountryOfBirthAggregator.php b/Export/Aggregator/CountryOfBirthAggregator.php index 43f62c336..071e0a783 100644 --- a/Export/Aggregator/CountryOfBirthAggregator.php +++ b/Export/Aggregator/CountryOfBirthAggregator.php @@ -28,13 +28,17 @@ use Symfony\Component\Translation\TranslatorInterface; use Chill\MainBundle\Util\CountriesInfo; use Symfony\Component\Security\Core\Role\Role; use Chill\PersonBundle\Security\Authorization\PersonVoter; +use Chill\MainBundle\Export\ExportElementValidatedInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; + /** * * * @author Julien Fastré */ -class CountryOfBirthAggregator implements AggregatorInterface +class CountryOfBirthAggregator implements AggregatorInterface, + ExportElementValidatedInterface { /** * @@ -83,6 +87,14 @@ class CountryOfBirthAggregator implements AggregatorInterface } + public function validateForm($data, ExecutionContextInterface $context) + { + if ($data['group_by_level'] === null) { + $context->buildViolation("You should select an option") + ->addViolation(); + } + } + public function alterQuery(QueryBuilder $qb, $data) { // add a clause in select part diff --git a/Export/Aggregator/NationalityAggregator.php b/Export/Aggregator/NationalityAggregator.php index 772564f7e..00838961d 100644 --- a/Export/Aggregator/NationalityAggregator.php +++ b/Export/Aggregator/NationalityAggregator.php @@ -28,13 +28,16 @@ use Symfony\Component\Translation\TranslatorInterface; use Chill\MainBundle\Util\CountriesInfo; use Symfony\Component\Security\Core\Role\Role; use Chill\PersonBundle\Security\Authorization\PersonVoter; +use Chill\MainBundle\Export\ExportElementValidatedInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * * * @author Julien Fastré */ -class NationalityAggregator implements AggregatorInterface +class NationalityAggregator implements AggregatorInterface, + ExportElementValidatedInterface { /** * @@ -82,6 +85,14 @@ class NationalityAggregator implements AggregatorInterface )); } + + public function validateForm($data, ExecutionContextInterface $context) + { + if ($data['group_by_level'] === null) { + $context->buildViolation("You should select an option") + ->addViolation(); + } + } public function alterQuery(QueryBuilder $qb, $data) { diff --git a/Export/Filter/BirthdateFilter.php b/Export/Filter/BirthdateFilter.php index 52afb2ff7..084b9ac8d 100644 --- a/Export/Filter/BirthdateFilter.php +++ b/Export/Filter/BirthdateFilter.php @@ -28,13 +28,14 @@ use Symfony\Component\Form\FormEvents; use Doctrine\ORM\Query\Expr; use Chill\MainBundle\Form\Type\Export\FilterType; use Symfony\Component\Form\FormError; +use Chill\MainBundle\Export\ExportElementValidatedInterface; /** * * * @author Julien Fastré */ -class BirthdateFilter implements FilterInterface +class BirthdateFilter implements FilterInterface, ExportElementValidatedInterface { public function addRole() @@ -82,38 +83,35 @@ class BirthdateFilter implements FilterInterface 'format' => 'dd-MM-yyyy', )); - $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { - /* @var $filterForm \Symfony\Component\Form\FormInterface */ - $filterForm = $event->getForm()->getParent(); - $enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData(); - - if ($enabled === true) { - // 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 ($date_from === null) { - $form->get('date_from')->addError(new FormError('This field ' - . 'should not be empty')); - } - if ($date_to === null) { - $form->get('date_to')->addError(new FormError('This field ' - . 'should not be empty')); - } - - // check that date_from is before date_to - if ( - ($date_from !== null && $date_to !== null) - && - $date_from >= $date_to - ) { - $form->get('date_to')->addError(new FormError('This date ' - . 'should be after the date given in "born after" field')); - } - } - }); + } + + public function validateForm($data, ExecutionContextInterface $context) + { + $date_from = $data['date_from']; + $date_to = $data['date_to']; + dump($date_from, $date_to); + + if ($date_from === null) { + $context->buildViolation('The "date from" should not be empty') + //->atPath('date_from') + ->addViolation(); + } + + if ($date_to === null) { + $context->buildViolation('The "date to" should not be empty') + //->atPath('date_to') + ->addViolation(); + } + + if ( + ($date_from !== null && $date_to !== null) + && + $date_from >= $date_to + ) { + $context->buildViolation('The date "date to" should be after the ' + . 'date given in "date from" field') + ->addViolation(); + } } public function describeAction($data, $format = 'string') diff --git a/Export/Filter/GenderFilter.php b/Export/Filter/GenderFilter.php index f321900e2..71089eeff 100644 --- a/Export/Filter/GenderFilter.php +++ b/Export/Filter/GenderFilter.php @@ -25,13 +25,16 @@ use Chill\PersonBundle\Entity\Person; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Query\Expr; use Symfony\Component\Security\Core\Role\Role; +use Chill\MainBundle\Export\ExportElementValidatedInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * * * @author Julien Fastré */ -class GenderFilter implements FilterInterface +class GenderFilter implements FilterInterface, + ExportElementValidatedInterface { public function applyOn() @@ -54,6 +57,14 @@ class GenderFilter implements FilterInterface )); } + public function validateForm($data, ExecutionContextInterface $context) + {dump($data); + if (count($data['accepted_genders']) === 0) { + $context->buildViolation("You should select an option") + ->addViolation(); + } + } + public function alterQuery(QueryBuilder $qb, $data) { $where = $qb->getDQLPart('where'); diff --git a/Export/Filter/NationalityFilter.php b/Export/Filter/NationalityFilter.php index cc0c0c7d3..e21ba2078 100644 --- a/Export/Filter/NationalityFilter.php +++ b/Export/Filter/NationalityFilter.php @@ -26,13 +26,16 @@ use Doctrine\ORM\Query\Expr; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\EntityRepository; use Chill\MainBundle\Entity\Country; +use Chill\MainBundle\Export\ExportElementValidatedInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * * * @author Julien Fastré */ -class NationalityFilter implements FilterInterface +class NationalityFilter implements FilterInterface, + ExportElementValidatedInterface { /** * @@ -58,6 +61,14 @@ class NationalityFilter implements FilterInterface } + public function validateForm($data, ExecutionContextInterface $context) + { + if ($data['nationalities'] === null) { + $context->buildViolation("A nationality must be selected") + ->addViolation(); + } + } + public function alterQuery(QueryBuilder $qb, $data) { $where = $qb->getDQLPart('where'); diff --git a/Resources/translations/validators.fr.yml b/Resources/translations/validators.fr.yml index d691c8119..d432cab5d 100644 --- a/Resources/translations/validators.fr.yml +++ b/Resources/translations/validators.fr.yml @@ -17,5 +17,15 @@ The birthdate must be before %date%: La date de naissance doit être avant le %d You must select at least one element: Vous devez sélectionner au moins un élément # filter by person birthdate -This field should not be empty: Ce champ ne peut pas être vide -This date should be after the date given in "born after" field: Cette date doit être après la date donnée du le champ "nés après le" +The "date to" should not be empty: Le champ "né avant cette date" ne peut pas être vide +The "date from" should not be empty: Le champ "né après cette date" ne peut pas être vide +The date "date to" should be after the date given in "date from" field: La date "né avant cette date" doit être antérieur à la date "né après cette date". + +# filter by nationality +A nationality must be selected: Une nationalité doit être choisie + +# aggregator by country, nationality +You should select an option: Une option doit être choisie. + +# aggregator by age +The date should not be empty: La date ne doit pas être vide