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

时间:2015-12-28 14:09:33

标签: php forms symfony twig

我的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';
    }
}

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

<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视图中呈现给我: enter image description here

如您所见,我在提交按钮上方有一个名为 __ name__label __ 的必需标签(位于表单顶部)和嵌入的表单标签专业

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

注意:如果我只使用{{ form_row(form.professions) }},我的twig中,我的professionType不会显示字段。 这是ProfessionType.php的代码:

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

1 个答案:

答案 0 :(得分:1)

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

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

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