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

2022-01-22 00:00:00 forms php symfony twig

我的 symfony 应用程序中有这个表格:

命名空间 MyNamespaceEntityBundleForm;使用 SymfonyComponentFormAbstractType;使用 SymfonyComponentFormFormBuilderInterface;使用 SymfonyComponentOptionsResolverOptionsResolverInterface;类 OrganizationType 扩展 AbstractType{/*** @param FormBuilderInterface $builder* @param 数组 $options*/公共函数 buildForm(FormBuilderInterface $builder, array $options){$建造者//职业复选框重叠->add('职业', '收藏', 数组('类型' =>新的专业类型(),'allow_add' =>true,//如果无法识别的项目被提交到集合中,它们将被添加为新项目'allow_delete' =>错误的,'by_reference' =>false,//为了调用加法器.'映射' =>真的,))->添加('名称')->add('siret')->add('公司');}/*** @param OptionsResolverInterface $resolver*/公共函数 setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(数组('data_class' =>'MyNamespaceEntityBundleEntityOrganization','csrf_protection' =>真的,'csrf_field_name' =>'_token_',//帮助生成秘密令牌的唯一密钥'意图' =>'organization_stuff',));}/*** @return 字符串*/公共函数 getName(){返回组织";}}

这就是我在树枝视图中呈现表单的方式:

{{ 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>

它在我的浏览器上的 html 视图中呈现这个:

如您所见,我有一个名为 __name__label__ 的必需标签(在表单的顶部)以及提交按钮上方的嵌入表单标签 Professions.

我该如何解决这个问题,或者自定义这个行为?

注意:在我的树枝中,如果我只使用 {{ form_row(form.professions) }},我的专业类型不会显示这些字段.这是ProfessionType.php的代码:

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

解决方案

我认为你有这些标签是因为你使用了 symfony 预定义的默认视图格式,你需要自定义它,另一个原因是你显示了嵌入表单原型,你需要将此原型设置为数据类型属性:

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

    I have this form in my symfony application:

    namespace MyNamespaceEntityBundleForm;
    
    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormBuilderInterface;
    use SymfonyComponentOptionsResolverOptionsResolverInterface;
    
    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' => 'MyNamespaceEntityBundleEntityOrganization',
                '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 ))
    ;
    

    解决方案

    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

    相关文章