Skip to content

[Form] Collection / allow_delete / removes items unwillingly #14938

Closed
@dmaicher

Description

@dmaicher

I think it might be a conceptual issue that I came across today. I created a minimal example to reproduce the issue:

https://github.com/dmaicher/symfony2-collection-problem

So basically its a simple form with a collection type:

class ConfigType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('options', 'collection', [
                'type' => new OptionType(),
                'allow_add' => true,
                'allow_delete' => true
            ])
            ->add('submit', 'submit')
        ;
    }

With this subform:

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('enabled', 'checkbox', [
                'required' => false
            ])
            ->add('name', 'text', [
                'disabled' => true
            ])
        ;
    }

As I set allow_delete to true one should be able to remove items client side. The problem is if I now uncheck the enabled checkbox for some of the options then the browser will not submit any values at all for those collection items (disabled field will be ignored & checkbox is not checked). So server side it seems like the whole form widget has been removed client side although that's not really the case here.

Anyone ever had this issue?

I guess the only solution for now is to add an additional unmapped field to the subform that will be submitted by the browser?

Like:

$form->add('_keep', 'hidden', [
    'mapped' => false,
    'data' => true
]);

Should this maybe be done automatically?

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