I'm facing a problem with Symfony 3.4 :
I have a formbuilder including 2 buttons ('add' and 'extract') and when 'add' is clicked, I want to add a new field in a CollectionType instead of doing the "extract" stuff.
I tried to do it with FormEvent but when I can tell which button isClicked() (SUBMIT), it's too late to call $event->setData() and $event->getForm()->setData() do nothing. On the other hand, when I can setData() (PRE_SUBMIT), I can't tell which button isClicked() since the information isn't computed yet in the submission process.
How can I achieve to add a value in my CollectionType (and so, a new field) after the 'add' button be clicked.
Thanks
$this->createFormBuilder(array('columns' => array(1, 2, 3)))
->add('columns', CollectionType::class, array(
'entry_type' => TextType::class
))
->add('extract', SubmitType::class)
->add('add', SubmitType::class)
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
if ($event->getForm()->get('add')->isClicked()) {
$data = $event->getData();
$data['columns'][] = null;
$event->setData($data);
}
})
->getForm()
->handleRequest($request)