簡體   English   中英

Symfony 2.8 / 3.0-將數組從collectionType傳遞到另一個formType

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

我可以在v2.8之前使它工作,但是由於symfony現在使用完全限定的類名,所以我不確定如何繼續。 我可以毫無問題地將數組(填充選擇字段)傳遞給表單,但是如果通過collectionType添加了另一個formType,如何傳遞數組呢?

順便說一句-數組是從自定義注釋的數據中收集的-不是實體這是我的代碼:

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
            ));
        }
    }

ActiveComponentsType嵌入表單確實可以工作-除非我不確定如何將$ componentChoices數組傳遞給它。

有任何想法嗎?

集合類型定義entry_options選項,該選項用於配置傳遞給嵌入式表單類型的選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM