Closed
Description
Right now, the addition of fields in event listeners is quite complicated, because FormInterface
does not support the same concise syntax for adding fields like FormBuilderInterface
does:
<?php
$formFactory = $builder->getFormFactory();
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
if (/* some condition */) {
$event->getForm()->add($formFactory->createNamed('myfield', 'text'));
} else {
$event->getForm()->remove('myfield');
}
});
This can be simplified by matching the signature of FormInterface::add()
with FormBuilderInterface::add()
:
<?php
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
if (/* some condition */) {
$event->getForm()->add('myfield', 'text');
} else {
$event->getForm()->remove('myfield');
}
});
The method, like FormBuilderInterface::add()
, would then support both signatures:
add($name, $type, array $options = array())
add(FormInterface $child)