简体   繁体   中英

Symfony FOSUserBundle cannot override form, no effect

with this article I'm trying to override RegisterFormType.php, and do not see the result, but Symfony Profiler shows in Forms fos_user_registration is overwritten by app_user_registration.

Firstly, I have in AppKernel.php:

new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

services.yml

app.form.registration:
    class: Application\Sonata\UserBundle\Form\RegistrationFormType
    tags:
        - { name: form.type, alias: app_user_registration }

config.yml:

fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User
    use_listener: true
    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager                    # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

    service:
        user_manager: sonata.user.orm.user_manager                      # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)
    registration:
        form:
            type: app_user_registration
            #validation_groups: [AppRegistration]
    profile:
        form:
            name: app_user_profile

src/Application/Sonata/UserBundle/Form/RegistrationForm.php:

namespace Application\Sonata\UserBundle\Form;

use ...

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

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


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

src/Application/Sonata/UserBundle/Entity/User.php:

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

class User extends BaseUser
{
    /**
     * @var int $id
     */
    protected $id;

    /**
     * @var string
     */
    protected $fullName;

    /**
     * @return string
     */
    public function getFullName() {
        return $this->fullName;
    }

    /**
     * @param string $fullName
     */
    public function setFullName($fullName) {
        $this->fullName = $fullName;
    }

    /**
     * Get id
     *
     * @return int $id
     */
    public function getId() { return $this->id;}
}

As other way (I think not correct) tried to override RegistrationController.php in my custom directory, changed there $form = $this->container->get('app.form.registration') (alias for my form), but I receive error Attempted to call an undefined method named "createView" of class "Application\\Sonata\\UserBundle\\Form\\RegistrationFormType . After this I changed this class' parent extended class from ContentAware to Controller, and can see form and new field, but after submitting receive unknown error, even in Profiler not written about it name.

Symfony 2.8.18, FOSUserBundle 1.3.7. Thank you for your time.

Did you update the configuration of the FOSUserBundle in config.yml ?

fos_user:
     # ...
     registration:
        form:
            name: app_user_registration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM