使用CollectionType时,Symfony3表单不显示输入字段

时间:2016-11-24 14:58:00

标签: twig symfony

使用CollectionType时显示表单时遇到问题。它不显示newOrderCustomerType输入,只标记“Customer Id”。怎么了?

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

在TWIG模板中

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

如何让它显示所有输入字段?

2 个答案:

答案 0 :(得分:1)

如果您有一个有效的“allow_add”选项,则可以通过“prototype”选项呈现此输入:

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

然后以表格形式:

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

它应该有用。

有关更多信息,请参阅prototype option

的Symfony文档

答案 1 :(得分:0)

尝试删除&#39; data_class&#39;,我不认为它是CollectionType的一部分:

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

看看是否有效。