加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 表单集合错误

发布时间:2020-12-13 13:37:28 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁上传.所以说上传30个文件,页面上有30个表格.我收到此错误: The form’s view data is expected to be of type scalar,array or an instance of ArrayAccess,but is an instance of class MS
我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁上传.所以说上传30个文件,页面上有30个表格.我收到此错误:

The form’s view data is expected to be of type scalar,array or an instance of ArrayAccess,but is an instance of class MSCoreBundleEntityPhoto. You can avoid this error by setting the “data_class” option to “MSCoreBundleEntityPhoto” or by adding a view transformer that transforms an instance of class MSCoreBundleEntityPhoto to scalar,array or an instance of ArrayAccess.

图库类型代码是:

public function buildForm(FormBuilderInterface $builder,array $options)
{
    $builder->add('photo','collection',array(
        'type' => new PhotoType(),'allow_add' => true,'data_class' => 'MSCoreBundleEntityPhoto','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' => 'MSCoreBundleEntityPhoto',));
    }

我的控制器功能是:

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


  }

任何帮助都会很棒.

您不应将data_class选项传递给GalleryType中的 collection type.或者,如果您想要覆盖PhotoType的默认值(已经设置,那么您不必这样做),您可以在options数组中指定它,如下所示:
public function buildForm(FormBuilderInterface $builder,'options' => array('data_class' => 'MSCoreBundleEntityPhoto'),));
}

确保你的“GalleryType”中设置了默认的data_class选项,它应该是一个专辑.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读