Created
May 26, 2016 11:45
-
-
Save ghoulaymen/bb9fb6ab42718464eec9a4bc7ef5c8b6 to your computer and use it in GitHub Desktop.
SF2, validation dynamique du formulaire avec les FORM EVENTS
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
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
$builder->add('date_debut', null, [ | |
'constraints' => [ | |
new Range(['min' => new \Datetime()) | |
] | |
] | |
); | |
$builder->add('date_fin', null, [ | |
'constraints' => [ | |
new Range(['min' => new \Datetime()) | |
] | |
] | |
); | |
$builder->addEventListener( | |
FormEvents::PRE_SUBMIT, | |
function (FormEvent $event) { | |
$data = $event->getData(); | |
if (array_key_exists('date_debut', $data) and array_key_exists('date_fin', $data) ) { | |
$event->getForm()->add('fate_fin', 'number', [ | |
'label' => 'date_fin', | |
'constraints' => [ | |
new Range(['min' => $data['date_debut']) | |
] | |
] | |
); | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment