Empty collection field type in form

I have defined a form that only has 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 the 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())
            );
}

      

But even if $ funciones has two entries, the form 'funciones' collection is empty, why? Did I miss something?

As you can see, I am a complete newbie with Symfony2, so please be patient with me.

Thanks in advance for your help!

+1


source to share


1 answer


What Symfony does with your current code:

  • take the $ functiones object (an instance of the Function class)
  • find the attribute functiones

    in the Function class
  • hydrate your form with the data found in this attribute

If you want to use the mappin object, you must specify your form field (c $builder->add('<name_here>')

) as an attribute of your Function class, which is a collection Function

.



Alternatively, you can try to moisten your shape with an array by specifying:

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

      

0


source







All Articles