add validation and translations

This commit is contained in:
2017-02-26 01:04:02 +01:00
parent 7a76868f6a
commit 13f3a1f55f
6 changed files with 76 additions and 17 deletions

View File

@@ -26,6 +26,7 @@ use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormError;
use Chill\MainBundle\Form\Type\Export\FilterType;
use Doctrine\ORM\Query\Expr;
use Symfony\Component\Translation\TranslatorInterface;
/**
*
@@ -34,6 +35,17 @@ use Doctrine\ORM\Query\Expr;
*/
class ActivityDateFilter implements FilterInterface
{
/**
*
* @var TranslatorInterface
*/
protected $translator;
function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function addRole()
{
@@ -93,12 +105,14 @@ class ActivityDateFilter implements FilterInterface
// check that fields are not empty
if ($date_from === null) {
$form->get('date_from')->addError(new FormError('This field '
. 'should not be empty'));
$form->get('date_from')->addError(new FormError(
$this->translator->trans('This field '
. 'should not be empty')));
}
if ($date_to === null) {
$form->get('date_to')->addError(new FormError('This field '
. 'should not be empty'));
$form->get('date_to')->addError(new FormError(
$this->translator->trans('This field '
. 'should not be empty')));
}
// check that date_from is before date_to
@@ -107,8 +121,10 @@ class ActivityDateFilter implements FilterInterface
&&
$date_from >= $date_to
) {
$form->get('date_to')->addError(new FormError('This date '
. 'should be after the date given in "activities after" field'));
$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')));
}
}
});

View File

@@ -30,13 +30,16 @@ use Symfony\Component\Security\Core\Role\Role;
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ActivityReasonFilter implements FilterInterface
class ActivityReasonFilter implements FilterInterface,
ExportElementValidatedInterface
{
/**
*
@@ -131,8 +134,16 @@ class ActivityReasonFilter implements FilterInterface
'expanded' => false
));
}
public function validateForm($data, ExecutionContextInterface $context)
{
if ($data['reasons'] === null || count($data['reasons']) === 0) {
$context->buildViolation("At least one reason must be choosen")
->addViolation();
}
}
public function getTitle()
public function getTitle()
{
return 'Filter by reason';
}

View File

@@ -32,13 +32,17 @@ use Chill\ActivityBundle\Entity\ActivityReason;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;
use Chill\PersonBundle\Export\Declarations;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PersonHavingActivityBetweenDateFilter implements FilterInterface
class PersonHavingActivityBetweenDateFilter implements FilterInterface,
ExportElementValidatedInterface
{
/**
@@ -53,12 +57,20 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
*/
protected $activityReasonRepository;
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct(
TranslatableStringHelper $translatableStringHelper,
EntityRepository $activityReasonRepository
EntityRepository $activityReasonRepository,
TranslatorInterface $translator
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->activityReasonRepository = $activityReasonRepository;
$this->translator = $translator;
}
@@ -158,12 +170,14 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
// check that fields are not empty
if ($date_from === null) {
$form->get('date_from')->addError(new FormError('This field '
. 'should not be empty'));
$form->get('date_from')->addError(new FormError(
$this->translator->trans('This field '
. 'should not be empty')));
}
if ($date_to === null) {
$form->get('date_to')->addError(new FormError('This field '
. 'should not be empty'));
$form->get('date_to')->addError(new FormError(
$this->translator->trans('This field '
. 'should not be empty')));
}
// check that date_from is before date_to
@@ -172,13 +186,22 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
&&
$date_from >= $date_to
) {
$form->get('date_to')->addError(new FormError('This date '
$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'));
. 'activity after this date" field')));
}
}
});
}
public function validateForm($data, ExecutionContextInterface $context)
{
if ($data['reasons'] === null || count($data['reasons']) === 0) {
$context->buildViolation("At least one reason must be choosen")
->addViolation();
}
}
public function describeAction($data, $format = 'string')
{