Skip to content

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

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy