Search code examples
symfonytwigformbuilder

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!


Solution

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