【问题标题】:Symfony 2.8/3.0 - Pass array to another formType from a collectionTypeSymfony 2.8/3.0 - 将数组从 collectionType 传递给另一个 formType
【发布时间】:2016-04-06 18:24:53
【问题描述】:

我可以在 v2.8 之前让它工作,但由于 symfony 现在使用完全限定的类名 name 我不确定如何继续。 我可以毫无问题地将数组(以填充选择字段)传递给表单,但是如果通过 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 数组传递给它。

有什么想法吗?

【问题讨论】:

    标签: php arrays symfony


    【解决方案1】:

    集合类型定义了entry_options 选项,用于配置传递给嵌入表单类型的选项。

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      相关资源
      最近更新 更多