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

前端 未结 8 769
半阙折子戏
半阙折子戏 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:24

    As of Sf 2.3, a quick workaround is to set the username to any string in the _construct of your class User that extends BaseUser.

    public function __construct()
        {
            parent::__construct();
            $this->username = 'username';
        }
    

    This way, the validator wont trigger any violation. But don't forget to set the email to the username as posted by Patt.

    public function setEmail($email)
    {
        $email = is_null($email) ? '' : $email;
        parent::setEmail($email);
        $this->setUsername($email);
    }
    

    You may have to check other files for references to User:username and change accordingly.

提交回复
热议问题