register birthdateValidator

in birthdate validator, improve the message
This commit is contained in:
Julien Fastré 2015-12-10 14:55:12 +01:00
parent 71eb972c4c
commit 04c56f667a
8 changed files with 58 additions and 7 deletions

View File

@ -24,8 +24,13 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// set configuration for double metaphone
$container->setParameter('cl_chill_person.search.use_double_metaphone',
$config['search']['use_double_metaphone']);
// set configuration for validation
$container->setParameter('chill_person.validation.birtdate_not_before',
$config['validation']['birthdate_not_after']);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

View File

@ -12,6 +12,10 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
*/
class Configuration implements ConfigurationInterface
{
private $validationBirthdateNotAfterInfos = "The period before today during which"
. " any birthdate is not allowed. The birthdate is expressed as ISO8601 : "
. "https://en.wikipedia.org/wiki/ISO_8601#Durations";
/**
* {@inheritDoc}
*/
@ -29,13 +33,34 @@ class Configuration implements ConfigurationInterface
->booleanNode('use_double_metaphone')
->defaultFalse()
->end()
->booleanNode('use_trigrams')->defaultFalse()->end();
->booleanNode('use_trigrams')
->defaultFalse()
->end()
->end()
->end()
->arrayNode('validation')
->canBeDisabled()
->children()
->scalarNode('birthdate_not_after')
->info($this->validationBirthdateNotAfterInfos)
->defaultValue('P1D')
->validate()
->ifTrue(function($period) {
try {
$interval = new \DateInterval($period);
} catch (\Exception $ex) {
return true;
}
return false;
})
->thenInvalid('Invalid period for birthdate validation : "%s" '
. 'The parameter should match duration as defined by ISO8601 : '
. 'https://en.wikipedia.org/wiki/ISO_8601#Durations')
->end()
->end()
->end();
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}

View File

@ -48,3 +48,11 @@ services:
tags:
- { name: security.voter }
- { name: chill.role }
chill.person.birthdate_validation:
class: Chill\PersonBundle\Validator\Constraints\BirthdateValidator
arguments:
- "%chill_person.validation.birtdate_not_before%"
tags:
- { name: validator.constraint_validator, alias: birthdate_not_before }

View File

@ -22,6 +22,8 @@ Chill\PersonBundle\Entity\Person:
- Date:
message: 'Birthdate not valid'
groups: [general, creation]
- Chill\PersonBundle\Validator\Constraints\Birthdate:
groups: [general, creation]
gender:
- NotNull:
groups: [general, creation]

View File

@ -11,3 +11,4 @@
'Closing date is not valid': 'La date de fermeture n''est pas valide'
'Closing date can not be null': 'La date de fermeture ne peut être nulle'
The date of closing is before the date of opening: La période de fermeture est après la période d'ouverture
The birthdate must be before %date%: La date de naissance doit être avant le %date%

View File

@ -38,7 +38,7 @@ class PersonControllerCreateTest extends WebTestCase
const CREATEDATE_INPUT = "chill_personbundle_person_creation[creation_date]";
const CENTER_INPUT = "chill_personbundle_person_creation[center]";
const LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq.";
const LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosq.";
/**
* return an authenticated client, useful for submitting form

View File

@ -34,4 +34,9 @@ use Symfony\Component\Validator\Constraint;
class Birthdate extends Constraint
{
public $message = "The birthdate must be before %date%";
public function validatedBy()
{
return 'birthdate_not_before';
}
}

View File

@ -38,6 +38,11 @@ class BirthdateValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if ($value === NULL) {
return;
}
if (!$value instanceof \DateTime) {
throw new \LogicException('The input should a be a \DateTime interface,'
. (is_object($value) ? get_class($value) : gettype($value)));
@ -47,7 +52,7 @@ class BirthdateValidator extends ConstraintValidator
if ($limitDate < $value) {
$this->context->buildViolation($constraint->message)
->setParameter('%date%', $value->format(\DateTime::ATOM))
->setParameter('%date%', $limitDate->format('d-m-Y'))
->addViolation();
}