【问题标题】:Form Collection Error表单收集错误
【发布时间】:2012-06-29 08:29:28
【问题描述】:

我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁上传。所以说要上传 30 个文件,页面上有 30 个表单。我收到此错误:

表单的视图数据应为标量、数组或\ArrayAccess 的实例类型,但它是MS\CoreBundle\Entity\Photo 类的实例。您可以通过将“data_class”选项设置为“MS\CoreBundle\Entity\Photo”或添加视图转换器来将 MS\CoreBundle\Entity\Photo 类的实例转换为标量、数组或 \数组访问。

画廊类型代码是:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('photo', 'collection', array(
        'type' => new PhotoType(),
        'allow_add' => true,
        'data_class' => 'MS\CoreBundle\Entity\Photo',
        'prototype' => true,
        'by_reference' => false,
    ));
}

照片类型代码是:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description', 'text', array('label' => "Title:", 'required' => true))
                ->add('File')
                ->add('album', 'entity', array(
                    'class' => 'MSCoreBundle:Album',
                    'property' => 'title',
                    'required' => true,
                    'query_builder' => function(EntityRepository $er)
                    {
                        return $er->createQueryBuilder('a')
                            ->orderBy('a.title', 'ASC');
                    },
                ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MS\CoreBundle\Entity\Photo',
        ));
    }

我的控制器功能是:

     public function newAction($count)
        {
            for($i = 1; $i <= $count; $i++) {
                $entity = new Photo();
            }

            $form = $this->container->get('ms_core.gallery.form');
            $form->setData($entity);

            return array(
                'entity' => $entity,
                'form' => $form->createView()
            );


  }

任何帮助都会很棒。

【问题讨论】:

    标签: php symfony symfony-forms


    【解决方案1】:

    您不应将 data_class 选项传递给 GalleryType 中的 collection type。或者,如果您确实想要覆盖 PhotoType 的默认值(已设置,因此您不必这样做),您可以在 options 数组中指定它,如下所示:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('photo', 'collection', array(
            'type' => new PhotoType(),
            'allow_add' => true,
            'options' => array('data_class' => 'MS\CoreBundle\Entity\Photo'),
            'prototype' => true,
            'by_reference' => false,
        ));
    }
    

    确保您确实在“GalleryType”中设置了默认的data_class 选项,它似乎应该是相册。

    另外,在您的控制器中,您没有正确创建表单。您需要使用表单的数据类型(在本例中为相册)调用 setData()

    public function newAction($count)
    {
            $album = new Album();
            for($i = 1; $i <= $count; $i++) {
                $album->addPhoto(new Photo());
            }
    
            $form = $this->container->get('ms_core.gallery.form');
            $form->setData($album);
    
            return array(
                'entity' => $album,
                'form' => $form->createView()
            );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 1970-01-01
      • 2022-08-05
      • 2015-09-02
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      相关资源
      最近更新 更多