Lilith
Lilith

Reputation: 165

Forms not showing input fields when using CollectionType

I'm having problem with displaying form when using CollectionType. It doesn't show AchatType inputs, just label "Achat".

Whats wrong? I don't understand..

enter image description here

My AchatType :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('civilite', ChoiceType::class, [
            'choices' => [
            'Mr' => 'Mr',
            'Mme' => 'Mme'
            ]])
        ->add('nom', TextType::class)
        ->add('prenom', TextType::class)
        ->add('commande', EntityType::class, array(
            'class' => 'AppBundle\Entity\Commande',
            ))
       ;
}

My CommandeType :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nb_ticket', ChoiceType::class, [
            'choices' => [
            1 => '1',
            2 => '2',
            3 => '3',
            4 => '4',
            5 => '5',
            6 => '6',
            ],
            'label' => false,
            'expanded' => true,     
            'multiple' => false,
            ])
        ->add('event', EntityType::class, array(
            'class' => 'AppBundle\Entity\Event',
            ))
        ->add('email', EmailType::class)
        ->add('achats', CollectionType::class, array(
            'entry_type' => AchatType::class,
        ))
        // ->add('achats', AchatType::class)
    ;
}

And in my Twig template I have :

<div class="text-center">
    {{ form_start(form) }}
    {% for index, nb_ticket in form.nb_ticket.children %}
        {{ form_widget(nb_ticket)}}
        {{ form_label(nb_ticket) }}
    {% endfor %}
</div>

<div style="display:none">
    {{ form_widget(form.event) }}
</div>
<!-- ======== ETAPE 2 ========  -->   
{% if form.achats.civilite is defined %}
    {{ form_widget(form.achats.civilite) }} 
    {{ form_widget(form.achats.prenom, {'attr':{ "placeholder":"Prenom" }}) }}
    {{ form_widget(form.achats.nom, {'attr':{ "placeholder":"Nom" }}) }}
<div style="display:none">
    {{ form_widget(form.achats.commande) }}
</div>
{% endif %}

<div class="text-white text-center">
    {{ form_widget(form.email, {'attr':{ "placeholder":"Email" }})}}
</div>
    {{ form_end(form) }}

the result is the same as if I do :

{{ form_start(form) }}
{{ form_end(form) }}

I notice that form.achats.civilite is not defined..

How to make it show all input fields or define form.achats.civilite and others fields in form.achats ?

Upvotes: 1

Views: 1897

Answers (1)

DaxDev
DaxDev

Reputation: 651

If you have an active "allow_add" option, it is possible to render this input throught the 'prototype' option:

->add('achats', CollectionType::class, array(
            'entry_type' => AchatType::class,
            'allow_add' => true,
            'prototype' => true,
        ))

and then in the form:

{{ form_row(form.achats.vars.prototype}) }}

It should work.

For more, see the Symfony documentation of prototype option

Upvotes: 2

Related Questions