如何在Symfony2表单中向CollectionType添加行?

时间:2016-09-08 15:41:33

标签: php forms symfony symfony-forms formcollection

我试图创建一个复杂的形式"使用Symfony2。这个表单的一个字段是从另一个"简单形式"创建的,并且表单的6个字段是从另一个"简单形式"创建的。 (所以这是一个重复的领域,是的)。它就像一种形式的表格"或嵌入式表格。

以下代码对应第一个"简单形式"这只是"复杂形式的一个领域":

<?php

namespace SisEvo\ConfiguracionBundle\Form;

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

class VariableReguladaElectricoValoresDatosType extends AbstractType{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
     public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('valor');          
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' =>     'SisEvo\ConfiguracionBundle\Entity\VariableReguladaElectricoValoresDatos'
        ));
    }

}

另一种简单形式&#34;这将以&#34;复杂形式重复6次&#34;:

<?php

namespace SisEvo\MaestroBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;


class VariablesMedidaType extends AbstractType{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array     $options) {
         $builder->add("medida", EntityType::class, array(
                    'required' => true,
                    'class' => 'MaestroBundle:VariablesMedida',
                    'choice_label' => 'medida',
                ));
    }

    /**
     * @param OptionsResolver $resolver
     */
     public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'SisEvo\MaestroBundle\Entity\VariablesMedida'
         ));
    }

}

最后,&#34;复杂形式&#34;:

<?php

namespace SisEvo\ConfiguracionBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use SisEvo\MaestroBundle\Form\VariablesMedidaType;
use SisEvo\ConfiguracionBundle\Form\VariableReguladaElectricoValoresDatosType;

class VariableReguladaElectricoValoresType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('idDatos', CollectionType::class, array(
                // each entry in the array will be an "email" field
                'entry_type' => VariableReguladaElectricoValoresDatosType::class,
                // these options are passed to each "email" type
                'entry_options' => array(
                    'attr' => array(
                        'rows' => 6
                    //,'class' => 'VariableReguladaElectricoValoresDatos'
                    )),
            ))
            //->add('valores', 'collection', array('type' => new VariableReguladaElectricoValoresDatosType()))
            ->add('idMedida', new VariablesMedidaType());
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'SisEvo\ConfiguracionBundle\Entity\VariableReguladaElectricoValores'
        , 'cascade_validation' => true
    ));
    }

}

idMedida字段工作正常,但我需要一些帮助来打印idDatos字段,应该打印6次。我尝试使用选项行来执行此操作,如您在代码中看到的那样,但是无效。

在twig文件中,我尝试使用{{ form_widget(myComplexForm) }}

打印表单

有什么想法解决它?

1 个答案:

答案 0 :(得分:0)

在Controller中创建表单时,您可以将SisEvo\ConfiguracionBundle\Entity\VariableReguladaElectricoValoresDatos的6个实体创建为ArrayCollection并将其设置为idDatos,然后将其作为setData方法传递给形式