表单中的空集合字段类型 [英] Empty collection field type in a form

查看:110
本文介绍了表单中的空集合字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个只有一个集合类型字段的表单:

I've defined a Form that only have one 'collection' type field:

<?php
namespace GMC\AccesoSistemaBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use GMC\AccesoSistemaBundle\Form\FuncionType;

class FuncionesType Extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder
                ->add('funciones', 'collection', array(
                    'type' => new FuncionType(),
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {

        $resolver->setDefaults(array(
            'data_class' => null
        ));
    }

    public function getName() {

        return 'gmc_accesosistemabundle_funcionestype';
    }

然后我在我的控制器中创建一个表单:

Then I create a form in my controller:

public function mostrarFuncionesAction() {
    $em = $this->getDoctrine()->getManager();
    $funciones = $em->getRepository('AccesoSistemaBundle:Funcion')->findAll();
    $formulario = $this->createForm(new FuncionesType(), $funciones);
    return $this->render(
            'AccesoSistemaBundle:Default:funciones.html.twig',
            array('formulario' => $formulario->createView())
            );
}

但是即使$ funciones有两个记录,'funciones'形式是空的,为什么?我缺少任何东西?

But even when $funciones has two records, the 'funciones' collection of the form is empty, why? Am I missing anything?

正如你可以看到,我是一个完整的新手Symfony2所以请耐心等待。

As you can see I'm a complete newbbie with Symfony2 so please be patient with me.

先感谢您的帮助!

推荐答案

Symfony使用您当前的代码是:

What Symfony is doing with your current code is :


  • 使用$ functiones对象(Function类的实例)

  • 查找属性

如果你想使用实体mappin,你必须命名你的表单字段( $ builder-> add('< name_here>') )作为您的Function类的属性,它是 Function 的集合。

If you want to use an entity mappin, you have to name your form field (with $builder->add('<name_here>')) as an attribute of your Function class, which is the collection of Function.

否则,您可以尝试使用数组水化你的形式,给出:

Else, you can try to hydrate your form with an array, giving :

$formulario = $this->createForm(new FuncionesType(), array('functiones' => $funciones));

这篇关于表单中的空集合字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆