hamzo
hamzo

Reputation: 195

Symfony3 forms not showing input fields when using CollectionType

I'm having problem with displaying form when using CollectionType. It doesn't show newOrderCustomerType inputs, just label "Customer Id". Whats wrong?

enter image description here

newOrderCustomerType

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class newOrderCustomerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->
        add('firstname', TextType::class,array('label'=>'Firstname'))->
        add('lastname', TextType::class,array('label'=>'Lastname'))->
        add('email', TextType::class,array('label'=>'Email'))->
        add('login', TextType::class,array('label'=>'Login'));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Customer',
        ));
    }

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

newOrderType

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class newOrderType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('customerId',CollectionType::class,array(
                'entry_type'=>newOrderCustomerType::class,
                'allow_add'    => true,
                'by_reference' => false,
                'data_class' => 'AppBundle\Entity\Customer',
            ))
            ->add('shopOrderId')
            ->add('orderDate')
            ->add('postBuyFormMsg')
            ->add('invoice')
            ->add('payType')
            ->add('shipmentType')
            ->add('payStatus')
            ->add('save',SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {

    }

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

And in TWIG template

{{ form_start(orderForm) }}
    {{ form_widget(orderForm) }}
{{ form_end(orderForm) }}

How to make it show all input fields?

Upvotes: 0

Views: 2283

Answers (2)

montie
montie

Reputation: 504

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

 $builder
        ->add('customerId',CollectionType::class,array(
            'entry_type'=>newOrderCustomerType::class,
            'allow_add'    => true,
            'by_reference' => false,
            'data_class' => 'AppBundle\Entity\Customer',
            'prototype'     => true,
        ))

and then in the form:

{{ form_row(orderForm.customerId.vars.prototype}) }}

It should work.

For more, see the Symfony documentation of prototype option

Upvotes: 2

Alvin Bunk
Alvin Bunk

Reputation: 7764

Try removing 'data_class', I don't think that's part of CollectionType:

->add('customerId',CollectionType::class,array(
     'entry_type'=>newOrderCustomerType::class,
     'allow_add'    => true,
     'by_reference' => false,
))

See if that works.

Upvotes: 0

Related Questions