dqtdz08206 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:

formulario

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)
    {
        $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());
    }
}

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog