Search code examples
symfonysymfony-2.3symfony-2.7

Form CollectionType with radio buttons fails after Symfony 2.7 upgrade


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

Solution

  • I fixed issue using below link....

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