Symfony 2.7升级后,带有单选按钮的Form CollectionType失败 [英] Form CollectionType with radio buttons fails after Symfony 2.7 upgrade
问题描述
得到以下错误.
在呈现模板的过程中引发了一个异常(表单的视图数据应为标量,数组或\ ArrayAccess的实例,但是是Proxies__CG __ \ BLA \ MyBundle \ Entity \您可以通过将"data_class"选项设置为"Proxies__CG __ \ BLA \ MyBundle \ Entity \ TransportType"或添加一个将类Proxies__CG __ \ BLA \ MyBundle \ Entity \ TransportType的实例转换为标量的视图转换器来避免此错误,数组或\ ArrayAccess的实例."),位于第8行的MyBundle:Shipping:form.html.twig中.
An exception has been thrown during the rendering of a template ("The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Proxies__CG__\BLA\MyBundle\Entity\TransportType. You can avoid this error by setting the "data_class" option to "Proxies__CG__\BLA\MyBundle\Entity\TransportType" or by adding a view transformer that transforms an instance of class Proxies__CG__\BLA\MyBundle\Entity\TransportType to scalar, array or an instance of \ArrayAccess.") in MyBundle:Shipping:form.html.twig at line 8.
$builder->add('variables','collection', array(
'type' => new AbcType(),
'options' => array(
'required' => true,
),
'constraints' => new NotNull()));
AbcType.php
class AbcType extends AbstractType
{
/**
* Build form
*
* @param FormBuilder $builder
* @param array $options
*
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('importance', null, array('empty_value'=>false,'expanded'=>true,
'required'=>true,'multiple'=>false,
'constraints' => new NotNull()))
->add('timeSpent', null, array(
'empty_value'=>false,'expanded'=>true,
'required'=>true,'multiple'=>false,
'constraints' => new NotNull()
)
);
}
/**
* setDefaultOptions set Default values
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Demo\MyBundle\Entity\Abc'
));
}
/**
* getName will return Form name
* @return string
*/
public function getName()
{
return 'demo_mybundle_abctype';
}
}
推荐答案
我使用以下链接解决了问题....
I fixed issue using below link....
https://github.com/symfony/symfony/issues/14877
这篇关于Symfony 2.7升级后,带有单选按钮的Form CollectionType失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!