Skip to content

[Form] CollectionType: prototype_data option creates duplicates instead of new lines #49961

Closed
@jkgroupe

Description

@jkgroupe

Symfony version(s) affected

6.2.8

Description

I'm using CollectionType to embed a collection of forms and when I'm using prototype_data option and there is a bug when I add new lines in my form. It creates duplicates instead of create x new lines.

FYI, i'm using DTO's to populate my entities, idk if it changes something but it was working before the package upgrade.
I don't have the issue when I removed the prototype_data option but i need it to initialize data in new fields

How to reproduce

My parent form

$builder
    ->add('name', TextType::class)
    ->add('steps', CollectionType::class, [
        'entry_type' => StepForm::class,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
        'prototype' => true,
        'prototype_name' => '__steps__',
        'prototype_data' => new StepData()
    ]);

My child form

$builder
    ->add('name', TextType::class)
    ->add('probability', ChoiceType::class, [
        'choice_loader' => new CallbackChoiceLoader(function () {
            $probabilities = [];

            for ($i = 0; $i <= 10; $i++) {
                $value = $i * 10;
                $probabilities["{$value}%"] = $value;
            }

            return $probabilities;
        }),
    ])
    ->add('position', HiddenType::class, [
        'attr' => ['class' => 'position']
    ]);

My form in front only the last 3 lines are added and new
Capture d’écran 2023-04-06 à 16 20 10

After form submission you can see that the last 3 lines are the same with the same id (2112)
Capture d’écran 2023-04-06 à 16 20 31

Possible Solution

I have downgraded the package symfony/form to the version 6.2.7 to solve this issue

Additional Context

I created a Symfony project to reproduce the bug. You can solve it to downgrade symfony/form package
https://github.com/jkgroupe/symfony-bug-form

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions