Symfony2 - renew or replace SonataUserBundle registration form

I want to expand or replace the registration type form in SonataUserBundle. I currently have a sonata user package that has been expanded with the easy-extends command. Therefore, I have a directory "src/Application/Sonata/UserBundle/"that was created.

I got the impression that if I create another file "RegistrationFormType.php"in "src/Application/Sonata/UserBundle/Form/Type/RegistrationFormType", this form will be downloaded, and not the one that is in the provider folder.

For example, I can easily override registerController by simply creating a file and class with the same name in my application folder to replace it in the provider folder. In addition, I can do the same to successfully replace some branch templates. However, this does not work for the form ...

The type of the form is as follows:

<?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 deleted the username field to see which form was loaded. However, when I access / register, I still get a username field, meaning that the uploaded form is not the one I’m looking for.

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

Edit 1: Added configurations

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
+4
source share
1 answer

, soanta , , . , .

,

   <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>

sonata,

registration:
    form:
        type:               acme_user_registration
        handler:            sonata.user.profile.form.handler.default
        name:               acme_user_registration_form
        validation_groups:  [AcmeUser]  
+2

Source: https://habr.com/ru/post/1534350/


All Articles