我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁上传.所以说上传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',)); }
我的控制器功能是:
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' => 'MS\CoreBundle\Entity\Photo'),)); }
确保你的“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() ); }