Remove / Replace the username field with email using FOSUserBundle in Symfony2 / Symfony3

前端 未结 8 774
半阙折子戏
半阙折子戏 2020-12-02 05:06

I only want to have email as mode of login, I don\'t want to have username. Is it possible with symfony2/symfony3 and FOSUserbundle?

I read here http://groups.google

8条回答
  •  一整个雨季
    2020-12-02 05:47

    Instead of Validation replacing I prefer to replace RegistrationFormHandler#process, more precisely add new method processExtended(for example), which is a copy of original method, and use ut in RegistrationController. (Overriding: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#next-steps)

    Before i bind register form i set username for example 'empty':

    class RegistrationFormHandler extends BaseHandler
    {
    
        public function processExtended($confirmation = false)
        {
            $user = $this->userManager->createUser();
            $user->setUsername('empty'); //That's it!!
            $this->form->setData($user);
    
            if ('POST' == $this->request->getMethod()) {
    
    
                $this->form->bindRequest($this->request);
    
                if ($this->form->isValid()) {
    
                    $user->setUsername($user->getEmail()); //set email as username!!!!!
                    $this->onSuccess($user, $confirmation);
    
                    /* some my own logic*/
    
                    $this->userManager->updateUser($user);
                    return true;
                }
            }
    
            return false;
        }
        // replace other functions if you want
    }
    

    Why? I prefer to user FOSUserBundle validation rules. Cuz if i replace Validation Group in config.yml for registration form i need to repeat validation rules for User in my own user entity.

提交回复
热议问题