Form CollectionType with radio buttons fails after Symfony 2.7 upgrade

∥☆過路亽.° 提交于 2019-12-23 02:39:11

问题


Getting below error..

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

回答1:


I fixed issue using below link....

https://github.com/symfony/symfony/issues/14877




回答2:


Make sure you have the 'data_class' default specified in the setDefaultOptions in the AbcType class:

... build form ...

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AbcBundle\Entity\Abc'
    ));
}


来源:https://stackoverflow.com/questions/31833637/form-collectiontype-with-radio-buttons-fails-after-symfony-2-7-upgrade

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