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

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

    You can make the username nullable and then remove it from the form type:

    First, in AppBundle\Entity\User, add the annotation above the User class

    use Doctrine\ORM\Mapping\AttributeOverrides;
    use Doctrine\ORM\Mapping\AttributeOverride;
    
    /**
     * User
     *
     * @ORM\Table(name="fos_user")
     *  @AttributeOverrides({
     *     @AttributeOverride(name="username",
     *         column=@ORM\Column(
     *             name="username",
     *             type="string",
     *             length=255,
     *             unique=false,
     *             nullable=true
     *         )
     *     ),
     *     @AttributeOverride(name="usernameCanonical",
     *         column=@ORM\Column(
     *             name="usernameCanonical",
     *             type="string",
     *             length=255,
     *             unique=false,
     *             nullable=true
     *         )
     *     )
     * })
     * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
     */
    class User extends BaseUser
    {
    //..
    

    When you run php bin/console doctrine:schema:update --force it will make the username nullable in the database.

    Second, in your form type AppBundle\Form\RegistrationType, remove the username from the form.

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
            $builder->remove('username');
            // you can add other fields with ->add('field_name')
        }
    

    Now, you won't see the username field in the form (thanks to $builder->remove('username');). and when you submit the form, you won't get the validation error "Please enter a username" anymore because it's no longer required (thanks to the annotation).

    Source: https://github.com/FriendsOfSymfony/FOSUserBundle/issues/982#issuecomment-12931663

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题