how to delete embedded form (collection field) lab

2020-07-27 06:19发布

问题:

I have this form in my symfony application:

namespace MyNamespace\EntityBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class OrganizationType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // profession checkboxes imbrication
            ->add('professions', 'collection', array(
                                                'type' => new ProfessionType(),
                                                'allow_add' => true,//  if unrecognized items are submitted to the collection, they will be added as new items
                                                'allow_delete' => false,
                                                'by_reference' => false, //in order that the adders are called.
                                                'mapped' => true,
                                            ))
            ->add('name')
            ->add('siret')
            ->add('corporation')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyNamespace\EntityBundle\Entity\Organization',
            'csrf_protection' => true,
            'csrf_field_name' => '_token_',
            // a unique key to help generate the secret token
            'intention'       => 'organization_stuff',
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'organization';
    }
}

And this how I render the form in my twig view:

<div>
  {{ form_start(form, {'action': path('path_action'), 'method': 'POST'}) }}
    {{ form_errors(form) }}
    {{ form_row(form.professions.vars.prototype) }}
    {{ form_row(form.name) }}
    {{ form_row(form.siret) }}
    {{ form_row(form.corporation) }}
  {{ form_end(form) }}
</div>

It renders me this in my html view on my browser:

As you can see I have a required label named __name__label__ (at the top of the form) and the embedded form label Professions above the submit button.

How can I fix that, or customize this behavior ?

Note: in my twig if I only use {{ form_row(form.professions) }}, my professionType does not display the fields. This is the code of ProfessionType.php :

$builder
    ->add('production', 'checkbox', array('required' => false ))
    ->add('transport', 'checkbox', array('required' => false ))
    ->add('pumping', 'checkbox', array('required' => false ))
;

回答1:

I think you are having those labels because you have used the default view format predefined by symfony you need to customize it , the other reason is that you have displayed the embedded form prototype, you need to set this prototype as data type attribute :

<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...

See http://symfony.com/doc/current/cookbook/form/form_collections.html