Search code examples
symfonysymfony-formssymfony4formbuilder

How to remove superfluous field-groups rendered?


I'm using the CollectionType to render multiple instances of a class in my form. But after the input fields are rendered there is superfluous information rendered.

Just like that: (Since i need at least 10 reputation to post images, i can only post the IMGUR-link)

https://i.sstatic.net/t5E7L.png

The controller:

$leadPartnerList = $LeadPartnerLoader->loadAll();
        $formBuilderData = [
            'lead_partners' => $leadPartnerList
        ];

        $form = $this->createFormBuilder($formBuilderData)
            ->add('lead_partners', CollectionType::class, [
                'entry_type' => LeadPartnerFormType::class,
                'entry_options' => [
                    'label' => null
                ]
            ])->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
            $data = $form->getData();
            dump($data);
        }

        return $this->render(
            'lead_partner_overview2.html.twig',
            [
                'form' => $form->createView()
            ]);

The LeadPartnerFormType:

class LeadPartnerFormType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => LeadPartner::class,
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('name', TextType::class);
    }
}

The Twig template:

<div>

        {{ form_start(form) }}
        {% for partner in form.lead_partners %}
            {{ form_row(partner.name) }}
        {% endfor %}
        {{ form_end(form) }}

    </div>

How to remove that excess stuff below the input fields? I'm a little lost with this one.(I suppose it has something to do with the array indices of the data supplied?)

Kind Regards


Solution

  • Thanks to the answer of Benjamin Kozlowski, i looked up form_rest(...):

    Changing the template to

     {{ form_start(form) }}
            {% for partner in form.lead_partners %}
                {{ form_row(partner.id) }}
                {{ form_row(partner.name) }}
            {% endfor %}
            {{ form_end(form) }}
    

    resolves the problem, because i forgot to render the hidden id fields, thus form_rest() renders them with a label (whysoever).