Form Collection Error

独自空忆成欢 提交于 2019-12-04 19:45:26

问题


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:

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.

The Gallery Type code is:

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

The Photo Type code is:

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',
        ));
    }

My Controller function is:

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


  }

Any help would be great.


回答1:


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

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

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


来源:https://stackoverflow.com/questions/11234149/form-collection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!