当前位置: 动力学知识库 > 问答 > 编程问答 >

symfony2 - auto div and field in Collection Form Symfony 2

问题描述:

i have a collection of form. When i render my result i have div and label for each Form ( when i use prototype for add form, these are also added)

first form is

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

}

}

The "main Form" is

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

}

}

In my Twig

<div id="form_foto">

{{ form_errors(form) }}

{{ form_rest(form) }}

in my dom i have this

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

Why i have this div of Default??

Thanks!

网友答案:

edit twig file

                        <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>
网友答案:

I Had the same issue, you can either modify twig (as suggested by Kaan) OR you can modify your type form as follow just by adding the options line:

    $builder                
            ->add('socials', 'collection', array(
                'type' => new SocialType(),
                'options' => array('label' => false), // ADD THIS LINE IN YOUR CODE
                'allow_add' => true, 
            ))
分享给朋友:
您可能感兴趣的文章:
随机阅读: