Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[CollectionType] Get binded data in FormType #19123

Closed
Labels
StalledhasPRA Pull Request has already been submitted for this issue.
@cavasinf

Description

@cavasinf

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    StalledhasPRA Pull Request has already been submitted for this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp