如何在symfony中删除嵌入式表单(集合字段)标签 [英] how to delete embedded form (collection field) label in symfony

查看:182
本文介绍了如何在symfony中删除嵌入式表单(集合字段)标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  namespace MyNamespace\EntityBundle\Form; 

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;

class OrganizationType extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
//行业复选框imbrication
- > add('professions','数组(
'type'=> new ProfessionType(),
'allow_add'=> true,//如果无法识别的项目被提交到集合,它们将被添加为新项目
'allow_delete'=> false,
'by_reference'=> false,//为了加法器被调用,
'mapped'=> true,
))
- > add('name')
- > add('siret')
- > add('corporation')
;

$ b $ **
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'MyNamespace\EntityBundle\Entity\Organization',
'csrf_protection'=> true,
'csrf_field_name'=>'_token_',
//帮助生成秘密令牌的唯一密钥
'intention'=>'organization_stuff',
));

$ b / **
* @return string
* /
public function getName()
{
return'组织';
}
}

这就是我如何在我的树枝视图中渲染窗体:

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

它在我的浏览器的html视图中呈现给我:



正如您所看到的,我有一个名为 __ name__label __ 的必需标签(位于表单顶部)嵌入式表单标签专业提交按钮上。



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

注意:如果我只使用 {{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预定义的默认视图格式需要定制它,另一个原因是您已经显示了嵌入式表单原型,您需要将此原型设置为数据类型属性:

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



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


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 ))
;

解决方案

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

这篇关于如何在symfony中删除嵌入式表单(集合字段)标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆