php - 表单收集错误

标签 php symfony symfony-forms

我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁。所以说要上传 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\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',
        ));
    }

我的 Controller 函数是:

     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 选项传递给 collection type在你的 GalleryType 中。或者,如果您确实想覆盖 PhotoType 的默认值(已经设置,因此您不必这样做),您可以在选项数组中指定它,如下所示:

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 选项,它似乎应该是一个专辑。

此外,在您的 Controller 中,您没有正确创建表单。您需要使用表单的数据类型调用 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()
        );
}

关于php - 表单收集错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234149/

相关文章:

php - 带有添加行按钮的 symfony2 表单

php - Symfony 2 连接不工作主义和 MySQL

php - 使用参数修改 symfony 表单的 url

php - 如果缺少中间文本,mysql 查询将不起作用

php - 在 Woocommerce 中为特定国家/地区自定义计算运输方式成本

php - 选择查询中的两级联接

php - 以字符串形式获取Symfony 2.7中的表单验证错误

symfony - 将 "help"消息添加到字段

php - 注册表单不会触发验证约束

php - 如何将 Doctrine 对象转换为 json