mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-11-04 03:08:25 +00:00 
			
		
		
		
	add validation on aggregators and filters
This commit is contained in:
		@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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')
 | 
			
		||||
 
 | 
			
		||||
@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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');
 | 
			
		||||
 
 | 
			
		||||
@@ -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é <julien.fastre@champs-libres.coop>
 | 
			
		||||
 */
 | 
			
		||||
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');
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user