Search code examples
phpsymfonydoctrine-ormfosuserbundlesymfony-forms

Could not override FOSUserBundle default register form


I'm new to symfony, I'm using symfony 2.8. I installed FOSUserBundle and it works just fine, the problem is that i want to add a field to the registration form I followed the steps in FOSUserBundle documentation But nothing has changed and I don't get an error. I don't know what exactly am I missing. This is my User entity:

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $name;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

This is the RegistrationType.php :

<?php
// src/AppBundle/Form/RegistrationType.php

namespace AppBundle\Form;

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

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

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

The app/config/services.yml file:

parameters:
    #parameter_name: value

services:
    app.form.registration:
        class: AppBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }

And this is the section related to FOSUserBundle in app/config/config.yml:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: AppBundle\Entity\User
    registration:
        form:
            name: app_user_registration

I couldn't find what I'm missing. Thank you.


Solution

  • Add to you form:

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

    if you are using Symfony < 2.8

    # app/config/config.yml
    fos_user:
        # ...
        registration:
            form:
                type: AppBundle\Form\RegistrationType
    

    if you are using Symfony > 2.8

    # app/config/config.yml
    fos_user:
        # ...
        registration:
            form:
                name: AppBundle\Form\RegistrationType