Skip to content

[Form]Inconsistent behavior in event listener #7873

@tkleinhakisa

Description

@tkleinhakisa

Hi,

i tried to remove an element dynamicaly from my form (as suggested possible in http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html : Example: you have a "Product" form and need to modify/add/remove a field based on the data on the underlying Product being edited.)

The form type:

$builder->add('object');
$builder->add('message');
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
    $form = $event->getForm();
    $form->remove('object');
});

Depending on how the form is created, the behavior is different:

$formFactory->create($formType, $data, $options)  // LISTENER IS CALLED BUT ELEMENT NOT REMOVED
$form = $formFactory->create($formType, null, $options) 
$form->setData($data)   // LISTENER IS CALLED AND ELEMENT IS REMOVED

I am using symfony 2.2

Thank you for feedback
Tom

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions