diff --git a/CHANGELOG.md b/CHANGELOG.md index 498ffa73e..adfe28335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Form/Type/DateIntervalType.php b/Form/Type/DateIntervalType.php index 499c61f93..c0fb741e8 100644 --- a/Form/Type/DateIntervalType.php +++ b/Form/Type/DateIntervalType.php @@ -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))); + } + }) + ; + } + } diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 545672126..393bf3d9b 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -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