add validation to aggregator and filters

This commit is contained in:
Julien Fastré 2017-03-07 16:03:49 +01:00
parent cc91ade6fe
commit 559239aca1
7 changed files with 55 additions and 6 deletions

View File

@ -22,6 +22,9 @@ namespace Chill\PersonBundle\Export\Aggregator;
use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Export\AggregatorInterface;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\DateType;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Translation\TranslatorInterface;
/** /**
* *
@ -30,6 +33,17 @@ use Symfony\Component\Form\Extension\Core\Type\DateType;
*/ */
class AgeAggregator implements AggregatorInterface class AgeAggregator implements AggregatorInterface
{ {
/**
*
* @var
*/
protected $translator;
public function __construct($translator)
{
$this->translator = $translator;
}
public function addRole() public function addRole()
{ {
@ -67,7 +81,7 @@ class AgeAggregator implements AggregatorInterface
} }
if ($value === NULL) { if ($value === NULL) {
return "no data"; return $this->translator->trans("without data");
} }
return $value; return $value;

View File

@ -160,7 +160,7 @@ class CountryOfBirthAggregator implements AggregatorInterface
// initialize array and add blank key for null values // initialize array and add blank key for null values
$labels[''] = $this->translator->trans('without data'); $labels[''] = $this->translator->trans('without data');
$labels['_header'] = $this->translator->trans('Nationality'); $labels['_header'] = $this->translator->trans('Country of birth');
foreach($countries as $row) { foreach($countries as $row) {
$labels[$row['c_countryCode']] = $this->translatableStringHelper->localize($row['c_name']); $labels[$row['c_countryCode']] = $this->translatableStringHelper->localize($row['c_name']);
} }
@ -177,7 +177,7 @@ class CountryOfBirthAggregator implements AggregatorInterface
'NA' => $this->translator->trans('North America'), 'NA' => $this->translator->trans('North America'),
'OC' => $this->translator->trans('Oceania'), 'OC' => $this->translator->trans('Oceania'),
'' => $this->translator->trans('without data'), '' => $this->translator->trans('without data'),
'_header' => $this->translator->trans('Continent') '_header' => $this->translator->trans('Continent of birth')
); );
} }

View File

@ -112,7 +112,7 @@ class CountPerson implements ExportInterface
} }
$labels = array_combine($values, $values); $labels = array_combine($values, $values);
$labels['_header'] = 'Number of people'; $labels['_header'] = $this->getTitle();
return function($value) use ($labels) { return function($value) use ($labels) {
return $labels[$value]; return $labels[$value];

View File

@ -20,13 +20,14 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider; use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\DateType;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
/** /**
* Render a list of peoples * Render a list of peoples
* *
* @author julien * @author julien
*/ */
class ListPerson implements ListInterface class ListPerson implements ListInterface, ExportElementValidatedInterface
{ {
/** /**
* *
@ -126,6 +127,26 @@ class ListPerson implements ListInterface
)); ));
} }
public function validateForm($data, ExecutionContextInterface $context)
{
dump($data);
// get the field starting with address_
$addressFields = array_filter(function($el) {
return substr($el, 0, 8) === 'address_';
}, $this->fields);
dump($data['address_date']);
// check if there is one field starting with address in data
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
// if a field address is checked, the date must not be empty
if (empty($data['address_date'])) {
$context
->buildViolation("You must set this date if an address is checked")
->atPath('address_date')
->addViolation();
}
}
}
/** /**
* Get custom fields associated with person * Get custom fields associated with person
* *

View File

@ -49,11 +49,19 @@ class GenderFilter implements FilterInterface
Person::FEMALE_GENDER => 'Woman', Person::FEMALE_GENDER => 'Woman',
Person::MALE_GENDER => 'Man' Person::MALE_GENDER => 'Man'
), ),
'multiple' => true, 'multiple' => false,
'expanded' => false 'expanded' => false
)); ));
} }
public function validateForm($data, ExecutionContextInterface $context)
{
if (count($data['accepted_genders']) === 0) {
$context->buildViolation("You should select an option")
->addViolation();
}
}
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$where = $qb->getDQLPart('where'); $where = $qb->getDQLPart('where');

View File

@ -60,5 +60,7 @@ services:
chill.person.export.aggregator_age: chill.person.export.aggregator_age:
class: Chill\PersonBundle\Export\Aggregator\AgeAggregator class: Chill\PersonBundle\Export\Aggregator\AgeAggregator
arguments:
- "@translator"
tags: tags:
- { name: chill.export_aggregator, alias: person_age_aggregator } - { name: chill.export_aggregator, alias: person_age_aggregator }

View File

@ -23,6 +23,7 @@ gender: genre
'Place of birth': 'Lieu de naissance' 'Place of birth': 'Lieu de naissance'
placeOfBirth: lieu de naissance placeOfBirth: lieu de naissance
'Country of birth': 'Pays de naissance' 'Country of birth': 'Pays de naissance'
'Continent of birth': Continent de naissance
countryOfBirth: 'Pays de naissance' countryOfBirth: 'Pays de naissance'
'Unknown country of birth': 'Pays inconnu' 'Unknown country of birth': 'Pays inconnu'
'Marital status': 'État civil' 'Marital status': 'État civil'
@ -185,3 +186,6 @@ Group people by gender: Aggréger les personnes par genre
Aggregate by age: Aggréger par âge Aggregate by age: Aggréger par âge
Calculate age in relation to this date: Calculer l'âge par rapport à cette date Calculate age in relation to this date: Calculer l'âge par rapport à cette date
Group people by country of birth: Aggréger les personnes par pays de naissance