Symfony 2.4 - 在表单中动态添加字段

Symfony 2.4 - Adding dinamically fields in the form

我使用 Symfony 2.4 创建了一个表单,该表单未链接到任何实体,因为我只想获取数据来创建报告。我使用 AbstractType extended class 创建了一个表单,我需要添加几个项目,因为该表单代表一个账单。我知道 allow_add 属性,但它只允许向表单添加一个字段,我需要做一些事情,就像我在图片中显示的那样:

我完全不知道该怎么做,我创建了一个项目 class,它包含两个属性,但我找不到任何相关信息。到目前为止,这是我得到的:

namespace Abadia\FacturaBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;>

class ReciboCajaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ciudad', 'text')
            ->add('fecha', 'date')
            ->add('valor', 'number')
            ->add('recibi_de', 'text')
            ->add('suma_recibida', 'number')
            ->add('suma_letras', 'textarea')
            ->add('bloque', 'text')
            ->add('numero', 'text')
            ->add('descripcion', 'textarea')
            ->add('areas_comunes', 'number')
            ->add('cuota_extraordinaria', 'number')
            ->add('saldo', 'number')
            ->add('cheque', 'number')
            ->add('otros', 'number')
            ->add('efectivo', 'number')
            ->add('generar', 'submit')
        ;
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array());
    }
}

在此先致谢。

更新: 我忘了说我正在使用 Twig 扩展。以防万一知道如何使用它。

基本上你需要2张表格。一个,将其称为主窗体,另一个称为项目的窗体。然后,您可以使用 collection type. You will need some javascript too for adding and removing an item. It would be very long to write down exactly how to do it, but there is a good example in the docs.

多次将项目表单类型嵌入到主表单类型中