Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ghoulaymen/bb9fb6ab42718464eec9a4bc7ef5c8b6 to your computer and use it in GitHub Desktop.
Save ghoulaymen/bb9fb6ab42718464eec9a4bc7ef5c8b6 to your computer and use it in GitHub Desktop.
SF2, validation dynamique du formulaire avec les FORM EVENTS
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