Php 如何在表单中添加动态字段?Symfony2

Php 如何在表单中添加动态字段?Symfony2,php,forms,symfony,twig,Php,Forms,Symfony,Twig,我的问题是下一个: 我有一个选择字段,用户在其中选择要买什么和要买多少,然后按下按钮“添加”,在返回显示选择字段。问题不在于如何做到。也就是说,如何使用数据控制器,例如修改类型等等。 你推荐什么?Symfony2如何处理这些情况 我的代码: ProdPedType.php: 我不确定,但我想你想看看。看看这个,我不确定,但这可能是你需要的 <?php namespace Proyecto\LavocBundle\Form; use Symfony\Component\Form\Abs

我的问题是下一个: 我有一个选择字段,用户在其中选择要买什么和要买多少,然后按下按钮“添加”,在返回显示选择字段。问题不在于如何做到。也就是说,如何使用数据控制器,例如修改类型等等。 你推荐什么?Symfony2如何处理这些情况

我的代码:

ProdPedType.php:


我不确定,但我想你想看看。看看这个,我不确定,但这可能是你需要的
<?php


namespace Proyecto\LavocBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ProdPedidoType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('producto', 'entity', array(
            'class' => 'ProyectoAdminBundle:Catalogo',
            'property' => 'articulo',

        ));

        $builder->add('cantidad', 'choice', array(
            'choices' => array(
                'empty_value' => 'Elija la cantidad',
                '1' => '1',
                '5' => '5',
                '10' => '10',
                '20' => '20',
                '50' => '50',
                '100' => '100',
                '150' => '150',
                '200' => '200'

            )
        ));
        $builder->add('valor', 'choice', array(
            'choices' => array(
                'empty_value' => 'Elija el peso',
                '1' => '1',
                '5' => '5',
                '10' => '10',
                '20' => '20',
                '50' => '50',
                '100' => '100'

            )
        ));
        $builder->add('unidad', 'choice', array(
            'choices' => array(
                'KGS' => 'KGS',
                'LTS' => 'LTS'
            )
        ));

    }

    public function getName() 
    {
        return 'prodpedido_form';
    }
{% extends 'AtajoBundle::layout.html.twig' %}

{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('bundles/MICARPETA/css/general.css') }}" type="text/css" rel="stylesheet">
<link href="{{ asset('bundles/MICARPETA/css/Pedido/crearPedido.css') }}" type="text/css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Spinnaker' rel='stylesheet' type='text/css'>
{% endblock %}

{% block content %}

<form action="{{ path('crear_pedido', { 'id': app.user.id } ) }}" method="post" {{ form_enctype(form) }}>
{{ form_start (form) }}
{{ form_errors(form) }}

    <div id="agrega">
    <ul>
        <li>{{ form_label(form.producto, 'Nombre del producto: ') }}</li>
        <li>{{ form_label(form.cantidad, 'Cantidad que va a llevar: ') }}</li>
        <li>{{ form_label(form.valor, 'Valor en numero (KGS/LTS): ') }}</li>
        <li>{{ form_label(form.unidad, 'KG o LTS: ') }}</li>

    </ul>

    <ul>
        <li>{{ form_widget(form.producto) }}</li>
        <li>{{ form_widget(form.cantidad) }}</li>
        <li>{{ form_widget(form.valor) }}</li>
        <li>{{ form_widget(form.unidad) }}</li>

    </ul>

    </div>



<div ><input id="agregarCampo" type="button" value="Agregar Producto"/></div>
<div ><input id="quitarCampo" type="button" value="Quitar Producto"/></div>
<div ><input  type="submit" id="submit"/></div>

{{ form_end (form) }}
{% endblock %}
public function pedidoAction()
    {
        $em = $this->getDoctrine()->getManager();

        $prodped= new ProdPedido();//creo la entidad de los productos
        $form = $this->createForm(new ProdPedidoType(), $prodped);

        //creo formulario del pedido que va a hacer



        return $this->render('AtajoBundle:General:pedido.html.twig', array('form' => $form->createView()));

    }