0

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! it's been a year since that issue. I managed to find a work around.Anyway thanks for answer.
@hamzo your question is actual until now))
0

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.

1 Comment

Unfortunately it doesn't. I tried adding and removing various options and still nothing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.