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

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

    As Michael points out, this can be solved with a custom validation group. For example:

    fos_user:
        db_driver: orm
        firewall_name: main
        user_class: App\UserBundle\Entity\User
        registration:
            form:
                type: app_user_registration
                validation_groups: [AppRegistration]
    

    Then in your entity (as defined by user_class: App\UserBundle\Entity\User) you can use the AppRegistration group:

    class User extends BaseUser {
    
        /**
         * Override $email so that we can apply custom validation.
         * 
         * @Assert\NotBlank(groups={"AppRegistration"})
         * @Assert\MaxLength(limit="255", message="Please abbreviate.", groups={"AppRegistration"})
         * @Assert\Email(groups={"AppRegistration"})
         */
        protected $email;
        ...
    

    This is what I ended up doing after posting that reply to the Symfony2 thread.

    See http://symfony.com/doc/2.0/book/validation.html#validation-groups for full details.

提交回复
热议问题