-
-
Save jonny-no1/70c920ab7f4f88a95b5ddd6640ab842c to your computer and use it in GitHub Desktop.
Symfony2: poor man's date_range form type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\OrmigoBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\Options; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Ormigo\Bundle\OrmigoBundle\Form\Validator\DateRangeValidator; | |
class DateRangeType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('start_date', 'date', array_merge_recursive([ | |
'property_path' => 'start', | |
'widget' => 'single_text', | |
'format' => 'yyyy-MM-dd', | |
'model_timezone' => 'UTC', | |
'view_timezone' => 'UTC', | |
'attr' => array( | |
'data-type' => 'start', | |
), | |
], $options['entry_options'])) | |
->add('end_date', 'date', array_merge_recursive([ | |
'property_path' => 'end', | |
'widget' => 'single_text', | |
'format' => 'yyyy-MM-dd', | |
'model_timezone' => 'UTC', | |
'view_timezone' => 'UTC', | |
'attr' => array( | |
'data-type' => 'end', | |
), | |
], $options['entry_options'])) | |
; | |
$builder->addEventSubscriber($options['validator']); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => '\DatePeriod', | |
'entry_options' => [], | |
'validator' => null, | |
)); | |
$resolver->setAllowedTypes([ | |
'validator' => 'Symfony\Component\EventDispatcher\EventSubscriberInterface', | |
]); | |
// Those normalizers lazily create the required objects, if none given. | |
$resolver->setNormalizers([ | |
'validator' => function (Options $options, $value) { | |
if (!$value) { | |
$value = new DateRangeValidator(new OptionsResolver()); | |
} | |
return $value; | |
}, | |
]); | |
} | |
public function getName() | |
{ | |
return 'date_range'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\OrmigoBundle\Form\Validator; | |
use DateTime; | |
use Symfony\Component\Form\FormError; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class DateRangeValidator implements EventSubscriberInterface | |
{ | |
public function onPostBind(FormEvent $event) | |
{ | |
$form = $event->getForm(); | |
$datePeriod = $form->getNormData(); | |
if ($datePeriod->getStartDate() > $datePeriod->getEndDate()) { | |
$form->addError(new FormError('date_range.invalid.end_before_start')); | |
} | |
} | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
FormEvents::POST_BIND => 'onPostBind', | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment