繁体   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