Question

I want to extend or replace the registration type form in SonataUserBundle. Currently, I have the sonata user bundle that was extended using the "easy-extends" command. Therefore, I have a directory "src/Application/Sonata/UserBundle/" that was created.

I was under the impression that if I created another file "RegistrationFormType.php" in "src/Application/Sonata/UserBundle/Form/Type/RegistrationFormType", that this form would be the one loaded, not the one in the vendor folder.

For example, I can easily override the registrationController by simply creating a file and a class with the same name in my Application folder to replace the one in the vendor folder. Also, I can do the same thing to successfully replace some twig templates. However, it does not work for the form...

The form type looks like this:

<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\UserBundle\Form\Type;

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

class RegistrationFormType extends AbstractType
{
    private $class;

    /**
     * @var array
     */
    protected $mergeOptions;

    /**
     * @param string $class        The User class name
     * @param array  $mergeOptions Add options to elements
     */
    public function __construct($class, array $mergeOptions = array())
    {
        $this->class        = $class;
        $this->mergeOptions = $mergeOptions;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('email', 'email', array_merge(array(
                'label' => 'form.email',
                'translation_domain' => 'SonataUserBundle',
            ), $this->mergeOptions))
            ->add('plainPassword', 'repeated', array_merge(array(
                'type' => 'password',
                'options' => array('translation_domain' => 'SonataUserBundle'),
                'first_options' => array_merge(array(
                    'label' => 'form.password',
                ), $this->mergeOptions),
                'second_options' => array_merge(array(
                    'label' => 'form.password_confirmation',
                ), $this->mergeOptions),
                'invalid_message' => 'fos_user.password.mismatch',
            ), $this->mergeOptions))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

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

Basically, I copied and pasted the file from "vendor/sonata/user-bundle/form/type/registrationFormType.php" but I removed the username field, just to see which form is loaded. However, when I access /register, I still get the username field meaning that the form loaded is not the want I'm looking for.

So, how do I overload or replace the form so that I can add the fields "firstName, lastName, dateOfBirth, etc.." so the user can enter them itself in the registration process.

Edit 1 : Configurations added

sonata_user:
    security_acl:           false

    manager_type: orm # Can be orm for mongodb

    table:
        user_group: "my_custom_user_group_association_table_name"

    impersonating:
        route:                page_slug
        parameters:           { path: / }

    class:                  # Entity Classes
        user:               Application\Sonata\UserBundle\Entity\User
        group:              Application\Sonata\UserBundle\Entity\Group

    admin:                  # Admin Classes
        user:
            class:          Sonata\UserBundle\Admin\Entity\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

        group:
            class:          Sonata\UserBundle\Admin\Entity\GroupAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

    profile:  # Profile Form (firstname, lastname, etc ...)
        form:
            type:               sonata_user_profile
            handler:            sonata.user.profile.form.handler.default
            name:               sonata_user_profile_form
            validation_groups:  [Profile]

        register:
            form:
                type:               osc_user_registration
                handler:            sonata.user.profile.form.handler.default
                name:               osc_user_registration_form
                validation_groups:  [SonataUser]

sonata_admin:
    templates:
        dashboard: SonataAdminBundle:Core:dashboard.html.twig
Was it helpful?

Solution

As you havent mentioned which soanta version are you using, I can't guess what is your configurations. Please post your configurations and sonata version.

Meanwhile Try on these line, You have to expose your form as a service

   <service id="acme_user.registration.form.type" class="Acme\UserBundle\Form\Type\RegistrationFormType">
            <tag name="form.type" alias="acme_user_registration" />
            <argument>%acme_user.model.user.class%</argument>
        </service>

and in under sonata config,

registration:
    form:
        type:               acme_user_registration
        handler:            sonata.user.profile.form.handler.default
        name:               acme_user_registration_form
        validation_groups:  [AcmeUser]  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top