Search code examples
formssymfonyregistrationfosuserbundle

Symfony overiding form fosuser omit getParent gives me error


I'm just reading FOSuserbundle overriding forms. It says that :

If you don't want to reuse the fields added in FOSUserBundle by default, you can omit the getParent method and configure all fields yourself.

When I do that, it gives me error.

Any help will be very appreciate.

My simple code

namespace CTC\Bundle\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // add form fields
        $builder->add('userCivility',null, array('label' => 'Title'));
        $builder->add('userFirstName');
    }

    // If you don't want to reuse the fields added in FOSUserBundle by default,
    // you can omit the getParent method and configure all fields yourself.
    // public function getParent()
    //{
    //    return 'fos_user_registration';
    //}

    public function getName()
    {
        return 'ctc_user_registration';
    }
}

Returns :

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class CTC\Bundle\UserBundle\Entity\User. You can avoid this error by setting the "data_class" option to "CTC\Bundle\UserBundle\Entity\User" or by adding a view transformer that transforms an instance of class CTC\Bundle\UserBundle\Entity\User to scalar, array or an instance of \ArrayAccess.


Solution

  • You may use the setDefault function:

    1. Add use statement.

      use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    2. Add function

      public function setDefaultOptions( OptionsResolverInterface $resolver )
      {
          $resolver->setDefaults( array(
              'data_class' => 'YamilovS\MyESchoolBundle\Entity\User',
              'intention'  => 'registration',
          ));
      }