mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 10:03:49 +00:00
allow to remove some units from date interval
This commit is contained in:
@@ -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)));
|
||||
}
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user