我正在尝试采用一种表单类型并显示它,但是我需要用户一次上传补丁上传。所以说要上传 30 个文件,页面上有 30 个表单。我收到此错误:
表单的视图数据应为标量、数组或 \ArrayAccess 的实例类型,但它是 MS\CoreBundle\Entity\Photo 类的实例。您可以通过将“data_class”选项设置为“MS\CoreBundle\Entity\Photo”或添加一个视图转换器来将 MS\CoreBundle\Entity\Photo 的实例转换为标量、数组或 \数组访问。
画廊类型代码是:
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()
);
}
任何帮助都会很棒。