MistaJase
MistaJase

Reputation: 849

Symfony 2.8/3.0 - Pass array to another formType from a collectionType

I could get this to work prior to v2.8 but as symfony now uses fully qualified class names name i'm unsure sure how to proceed. I can pass an array (to populate a choice field) to a form without issue but if there is an another formType added via a collectionType how can a pass the array?

BTW - the array is gathered from data from a custom annotations - NOT an entity Heres my code:

PageType.php

    <?php
    namespace Prototype\PageBundle\Form;

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

    const ActiveComponentsType = 'Prototype\PageBundle\Form\ActiveComponentsType';
    const collectionType = 'Symfony\Component\Form\Extension\Core\Type\CollectionType';

    class PageType extends AbstractType
    {

        private $cmsComponentArray;

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

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

            $cmsComponentArray = $options['cmsComponentArray'];

            $componentChoices = array();
            foreach($cmsComponentArray as $cmsComponent){
                $componentChoices[$cmsComponent['name']] = $cmsComponent['route'];
            }

            //correct values are shown here
            //print_r($componentChoices);

            $builder
                ->add('title')
                ->add('parent')
                ->add('template')
                ->add('active')
                ->add('content')
                ->add('components', collectionType, array(
                    'entry_type'   => ActiveComponentsType, // i want to pass $cmsComponentArray to ActiveComponentsType 
                    'allow_add' => true,
                    'allow_delete' => true
                ))

            ;
        }

        /**
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Prototype\PageBundle\Entity\Page',
                'cmsComponentArray' => null
            ));
        }
    }

The ActiveComponentsType embeded form does work - except I'm unsure how to pass the $componentChoices array to it.

Any ideas?

Upvotes: 1

Views: 1288

Answers (1)

xabbuh
xabbuh

Reputation: 5881

The collection type defines the entry_options option which is used to configure the options that are passed to the embedded form type.

Upvotes: 1

Related Questions