[英]How to properly set 'data_class' for form collection in Symfony2?
我想收集一些表格。 我的积木形式定义如下:
class ImportPaymentsType extends AbstractType
{
public function getName()
{
return 'import_payments_type';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('caisse', 'entity', array(
'label' => null,
'class' => 'ACME\AppBundle\Entity\Caisse',
'property' => 'name',
'empty_value' => 'Choose an option',
))
->add('label', 'text', array(
'label' => null,
))
->add('credit', 'number', array(
'label' => null,
));
}
}
在控制器中,我构造如下形式:
$data = array(
'imports' =>
array(
'caisse' => $em->getRepository('ACMEAppBundle:Caisse')->findOneByCode('P6015C'),
'label' => null,
'credit' => null,
)
);
$importForm = $this->createFormBuilder($data)
->add('imports', 'collection', array(
'type' => new ImportPaymentsType(),
))->getForm();
我得到的错误是:
表单的视图数据应为标量类型,数组类型或\\ ArrayAccess实例,但为ACME \\ AppBundle \\ Entity \\ Caisse类的实例。 您可以通过将“ data_class”选项设置为“ ACME \\ AppBundleBundle \\ Entity \\ Caisse”或添加将View类ACME \\ AppBundleBundle \\ Entity \\ Caisse转换为标量,数组或\\的实例的视图转换器来避免此错误。 ArrayAccess。
我试图更改ImportPaymentsType,但没有成功:
->add('caisse', 'entity', array(
'label' => null,
'class' => 'ACME\AppBundle\Entity\Caisse',
'property' => 'description',
'empty_value' => 'Choose an option',
'data_class' => 'ACME\AppBundle\Entity\Caisse'
))
我该怎么办? 请帮忙。
'imports'
应该是一个数组数组。
尝试:
$data = array(
'imports' =>
array(
array(
'caisse' => $em->getRepository('ACMEAppBundle:Caisse')->findOneByCode('P6015C'),
'label' => null,
'credit' => null,
)
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.