symfony: $event->getData() returns null for an embedded form
Related with #5694
Using $event->getData()
within an event for an embedded form return null.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::POST_SET_DATA, function ($event) {
die(var_dump($event->getData())); //Return NULL for embedded forms, and the entity for non-embedded forms
});
}
I have tested it for a non-persisted entity with properties assigned in the controller. But it should be the same for persisted entities, I suppose.
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 36 (10 by maintainers)
This is rather annoying to have to disable allow_add as, right now, I have to allow adds.
After having given a better insight, I noticed that the child form is called for each item of your collection, plus called the first time without any argument nor data.
The quick fix I found is to test if getData() is null or not. Works for me with allow_add => true.
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$event->getData() is null and it shouldn’t be.