Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
Closed
Description
I don't know if it is an expected behavior,
but when using anEmbed Collection of Forms,
when you try to access the bound data with$builder->getData()
, the result is null.
// src/Form/TaskType.phppublicfunctionbuildForm(FormBuilderInterface$builder,array$options):void{$builder->add('tags', CollectionType::class, ['entry_type' => TagType::class, ]);}// src/Form/TagType.phppublicfunctionbuildForm(FormBuilderInterface$builder,array$options):void{$builder->getData();// <-- Is null}publicfunctionconfigureOptions(OptionsResolver$resolver):void{$resolver->setDefaults(['data_class' => Tag::class, ]);}
You need to go through the PRE_SET_DATA eventListener to get the data with$event->getData()
.
// src/Form/TagType.phppublicfunctionbuildForm(FormBuilderInterface$builder,array$options):void{$builder->addEventListener(FormEvents::PRE_SET_DATA,function(FormEvent$event) {$event->getData();// <-- Is OK });}publicfunctionconfigureOptions(OptionsResolver$resolver):void{$resolver->setDefaults(['data_class' => Tag::class, ]);}
Even worth, when the form (which call the CollectionType) sets theby_reference
to false,
the entire form must to be inside a PRE_SET_DATA event.
// src/Form/TaskType.phppublicfunctionbuildForm(FormBuilderInterface$builder,array$options):void{$builder->add('tags', CollectionType::class, ['entry_type' => TagType::class,'by_reference' =>false,// $event->getData() inside TagType::class will be null ]);}
// src/Form/TaskType.phppublicfunctionbuildForm(FormBuilderInterface$builder,array$options):void{$builder->addEventListener(FormEvents::PRE_SET_DATA,function(FormEvent$event) {$form =$event->getForm();$form->add('tags', CollectionType::class, ['entry_type' => TagType::class,'by_reference' =>false,// $event->getData() is OK ]); });}
If it is a thing,
I think it is worth mentioning thatsomewhere in the doc.