Collection Form Symfony 2中的auto div和field

时间:2013-02-03 20:31:52

标签: symfony twig formbuilder

我有一个表格集合。当我渲染我的结果时,我有每个表格的div和标签(当我使用原型添加表格时,这些也被添加) 第一种形式是

namespace Webwall\CamBundle\Form;

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

class SocialType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder

                ->add('account')

        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Webwall\CamBundle\Entity\Social',
        ));
    }

    public function getName() {
        return 'social';
    }

}

“主要表格”是

namespace Webwall\CamBundle\Form;

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

class MediaType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder                
                ->add('socials', 'collection', array(
                    'type' => new SocialType(),
                    'allow_add' => true, 
                ))

        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Webwall\CamBundle\Entity\Media'
        ));
    }

    public function getName() {
        return 'foto';
    }

}

在我的小树枝中

<div id="form_foto">
{{ form_errors(form) }}
{{ form_rest(form) }}

在我的dom中我有这个

<div><label class="required">Socials</label><div id="foto_socials" data-prototype=""><div><label class="required">0</label>

为什么我有这个默认的div? 谢谢!

2 个答案:

答案 0 :(得分:1)

编辑树枝文件

                        <ul class="unstyled child" data-prototype="{{ form_widget(form.socials.vars.prototype)|e }}">
                            {% for row in form.socials %}
                                <li style="margin-bottom: 5px;"> {{ form_widget(row.account) }}</li>
                            {% endfor %}
                        </ul>

答案 1 :(得分:0)

我有同样的问题,您可以修改 twig (按照Kaan的建议)您可以修改您的类型表单,只需按添加选项行

    $builder                
            ->add('socials', 'collection', array(
                'type' => new SocialType(),
                'options' => array('label' => false), // ADD THIS LINE IN YOUR CODE
                'allow_add' => true, 
            ))