Rendering form with custom field type of entity collection in Symfony2

3k views Asked by At

I am still working on small challenge with category nested tree form output and submission: Symfony2 entity collection populating selectboxes

And I am trying to find out the rendering part first (before JS and populating).

I have a general form:

CategoryForm

class CategoryForm extends AbstractType
{
    private $em;

    public function __construct($em) {
       $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
      $transformer = new CategoryToChoiceTransformer($this->em);

      $builder->add(
               $builder->create('categories', 'collection',
                   array(
                      'type' => new CategoryCollectionType($this->em)
                   )
               )->addModelTransformer($transformer));



        $builder->add('save', 'submit');
    }

    public function getDefaultOptions(array $options)
    {
        $resolver->setDefaults(array(
                'data_class' => 'Acme\DemoBundle\Entity\Item',
        ));

    }

    public function getName()
    {
        return 'category';
    }
}

I have CategoryToChoiceTransformer:

CategoryToChoiceTransformer

class CategoryToChoiceTransformer implements DataTransformerInterface
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @param ObjectManager $om
     */
    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function transform($categories)
    {
        $categoriesExpanded = new ArrayCollection();

        if (!empty($categories)) {
            $categoryRepository = $this->om->getRepository('AcmeDemoBundle:Category');

            foreach ($categories as $category) {
                $path = new ArrayCollection($categoryRepository->getPath($category));
                $categoriesExpanded->add($path);
            }
        }

        return $categoriesExpanded;
    }

    public function reverseTransform($categoryList)
    {
        ...
    }
}

And I have custom type (which I use in parent form):

CategoryCollectionType

class CategoryCollectionType extends AbstractType
{
    private $em;

    public function __construct($em) {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('name', 'entity', array(
                'class' => 'AcmeDemoBundle:Category',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                        ->orderBy('c.id', 'ASC');
                 },
                'property' => 'name',
                'empty_value' => _('Choose Category')
        ));


    }

    public function getName()
    {
        return 'category_collection_type';
    }
}

But what I got is: enter image description here

But I expect to see 3 selectboxes in first group and 3 selecboxes in second (data transformer transforms data from:

array(
    [0] = >ObjectCategory#...,
    [1] => ObjectCategory#...
)

to

array(
    [0] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., ),
    [1] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., )
)

Where is my mistake?

I thought I should do the collection type in the CategoryCollectionType:

CategoryCollectionType

...
$builder->add('categories', 'collection', array(
    'type' => 'entity',
    'allow_add' => true,
    'allow_delete' => true,
    'prototype' => false,
    'show_legend' => true,
    'widget_add_btn' => array('label' => _('Add category')),
    'options' => array(
                    'widget_control_group' => false,
                    'label_render' => false,
                    'show_legend' => false,
                    'class' => 'AcmeDemoBundle:Category',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                            ->orderBy('c.id', 'ASC');
                    },
                    'property' => 'name',
                    'empty_value' => _('Choose Category'),
                ),
    )
);

But it doesn't help... Any ideas?

P.S> Please, keep follow this thread if you answer :)

0

There are 0 answers