allow to remove some units from date interval

This commit is contained in:
Julien Fastré 2019-05-09 16:10:35 +02:00
parent c1b8de4ee5
commit d2895bfb7c
3 changed files with 65 additions and 6 deletions

View File

@ -50,3 +50,7 @@ Version 1.5.7
- add css layout for boxes ;
- collect supplementary query parameters in SearchController ;
Version 1.5.8
=============
- allow to remove interval units from DateInterval

View File

@ -23,9 +23,38 @@ use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Chill\MainBundle\Form\Type\DataTransformer\DateIntervalTransformer;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
/**
* Show a dateInterval type
*
* Options:
*
* - `unit_choices`: an array of available units choices.
*
* The oiriginal `unit_choices` are :
* ```
* [
* 'Days' => 'D',
* 'Weeks' => 'W',
* 'Months' => 'M',
* 'Years' => 'Y'
* ]
* ```
*
* You can remove one or more entries:
*
* ```
* $builder
* ->add('duration', DateIntervalType::class, array(
* 'unit_choices' => [
* 'Years' => 'Y',
* 'Months' => 'M',
* ]
* ));
* ```
*
*/
class DateIntervalType extends AbstractType
@ -42,16 +71,39 @@ class DateIntervalType extends AbstractType
]
])
->add('unit', ChoiceType::class, [
'choices' => [
'Days' => 'D',
'Weeks' => 'W',
'Months' => 'M',
'Years' => 'Y'
],
'choices' => $options['unit_choices'],
'choices_as_values' => true
])
;
$builder->addModelTransformer(new DateIntervalTransformer());
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefined('unit_choices')
->setDefault('unit_choices', [
'Days' => 'D',
'Weeks' => 'W',
'Months' => 'M',
'Years' => 'Y'
])
->setAllowedValues('unit_choices', function($values) {
if (FALSE === is_array($values)) {
throw new InvalidOptionsException("The value `unit_choice` should be an array");
}
$diff = \array_diff(\array_values($values), ['D', 'W', 'M', 'Y']);
if (count($diff) == 0) {
return true;
} else {
throw new InvalidOptionsException(sprintf("The values of the "
. "units should be 'D', 'W', 'M', 'Y', those are invalid: %s",
\implode(', ', $diff)));
}
})
;
}
}

View File

@ -39,6 +39,9 @@ Edit: Modifier
Update: Mettre à jour
Back to the list: Retour à la liste
#interval
Years: Années
#elements used in software
centers: centres
Centers: Centres