Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CollectionType] Get binded data in FormType #19123

Open
cavasinf opened this issue Nov 9, 2023 · 0 comments
Open

[CollectionType] Get binded data in FormType #19123

cavasinf opened this issue Nov 9, 2023 · 0 comments
Labels
hasPR A Pull Request has already been submitted for this issue.

Comments

@cavasinf
Copy link
Contributor

cavasinf commented Nov 9, 2023

I don't know if it is an expected behavior,
but when using an Embed Collection of Forms,
when you try to access the bound data with $builder->getData(), the result is null.

// src/Form/TaskType.php

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder->add('tags', CollectionType::class, [
        'entry_type' => TagType::class,
    ]);
}

// src/Form/TagType.php

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder->getData(); // <-- Is null
}

public function configureOptions(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.php

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
         $event->getData(); // <-- Is OK
    });
}

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => Tag::class,
    ]);
}

Even worth, when the form (which call the CollectionType) sets the by_reference to false,
the entire form must to be inside a PRE_SET_DATA event.

// src/Form/TaskType.php

public function buildForm(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.php

public function buildForm(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 that somewhere in the doc.

@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

2 participants