2016-02-20 21:35
浏览 71

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

I have created a form using Symfony 2.4 that is not linked to any entity because I only want to take the data to create a report. I have created a form using an AbstractType extended class and I need to add several items since the form represents a bill. I know about the allow_add attribute, but it just lets to add a field to the form and I need to do something like I show in the image:


I have no idea at all about how to do it, I have created an item class, and it contains two attributes, but I find nowhere any information about this. Until now this is what I've got:

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)
            ->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)

Thanks in advanced.

UPDATE: I forgot to mention that I am working with the Twig extension. Just in case knows how to do it using it.

图片转代码服务由CSDN问答提供 功能建议

我使用Symfony 2.4创建了一个没有链接到任何实体的表单,因为我只想将数据带到 创建一个报告。 我使用AbstractType扩展类创建了一个表单,我需要添加几个项目,因为表单代表一个帐单。 我知道allow_add属性,但它只是允许在表单中添加一个字段,我需要做一些像我在图像中显示的那样:

我根本不知道怎么做,我创建了一个项目类,它包含两个属性 ,但我找不到任何关于此的信息。 到目前为止,这就是我所拥有的:

 命名空间Abadia \ FacturaBundle \ Form; 
use Symfony \ Component \ Form \ AbstractType; 
use Symfony \ Component \ Form  \ FormBuilderInterface; 
use Symfony \ Component \ OptionsResolver \ OptionsResolverInterface;> 
class ReciboCajaType扩展AbstractType 
公共函数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('check','number')
  - > ad  d('otros','number')
  - > add('efectivo','number')
  - > add('generar','submit')
  public function getName()
公共函数setDefaultOptions(OptionsResolverInterface $ resolver)
 $ resolver-> setDefaults(array()); 


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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtrhd2850 2016-02-20 21:52

    Basically you will need 2 forms. One, call it main form and an another form for an item. Then you can embed the item form type into the main form type multiple times using the 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.

    打赏 评论

相关推荐 更多相似问题