简体   繁体   English

如何在symfony中删除嵌入的表单(集合字段)标签

[英]how to delete embedded form (collection field) label in symfony

I have this form in my symfony application: 我的symfony应用程序中有以下表格:

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: 它在浏览器的html视图中将其呈现给我: 在此处输入图片说明

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. 如您所见,我有一个名为__name__label__的必需标签(在表单顶部),并且在“提交”按钮上方有一个嵌入的表单标签Professions

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. 注意:在我的树枝中,如果仅使用{{ form_row(form.professions) }} ,我的行业类型将不显示字段。 This is the code of ProfessionType.php : 这是ProfessionType.php的代码:

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

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 : 我认为您拥有这些标签是因为您使用了symfony预定义的默认视图格式,需要对其进行自定义,另一个原因是您已经显示了嵌入式表单原型,因此需要将此原型设置为data type属性:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM