表单收集错误 [英] Form Collection Error

查看:19
本文介绍了表单收集错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to take one form type and display it however times i need for the user to upload a patch upload at one time. So say 30 files to uploaded, 30 forms on the page. I am receiving this error:

表单的视图数据应该是标量、数组或\ArrayAccess 的一个实例类型,但它是MS\CoreBundle\Entity\Photo 类的一个实例.您可以通过将data_class"选项设置为MS\CoreBundle\Entity\Photo"或通过添加将类 MS\CoreBundle\Entity\Photo 的实例转换为标量、数组或 \ 的实例的视图转换器来避免此错误数组访问.

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\CoreBundle\Entity\Photo. You can avoid this error by setting the "data_class" option to "MS\CoreBundle\Entity\Photo" or by adding a view transformer that transforms an instance of class MS\CoreBundle\Entity\Photo 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' => '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()
            );


  }

任何帮助都会很棒.

推荐答案

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

You should not pass the data_class option to the collection type in your GalleryType. Or, if you do want to override the PhotoType's default (which is already set, so you shouldn't have to), you can specify it in the options array like so:

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选项,它似乎应该是一个相册.

Make sure you do have a default data_class option set in your "GalleryType", it should be an Album, it seems.

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

Also, in your controller you are not creating the form correctly. You need to call setData() with the data type of the form, in this case an 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()
        );
}

这篇关于表单收集错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆