Momo1987
Momo1987

Reputation: 544

avoid username by registration in FOSuserBundle

When I try to register two user with the same name.I get the message that this name exists already with fosbundle.My question is how to avoid that?

Upvotes: 0

Views: 196

Answers (2)

Brendan
Brendan

Reputation: 978

If you want to allow multiple users to have the same username, what you will need to do is override the FOSUserBundle controller and set a new field in the registration page. I would recommend removing the 'username' field and create your new 'name' property.

So add a name called 'user_name' which acts the same way as first name and last name. Remove the username field from the registration form as this has to be unique.

You can learn more about this here: http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_forms.html

When a user is created using FOSUserBundle they must require the username, email and password. Because you have removed the username field, I would just duplicate the email as the username (because you need at least one field that is unique).

If you override the login form so that it only checks for email and password for validation rather than username/email and password, it should give you your desired functionality of logging in via email and password only. As username will also be their email.

Example of the new registration page when you have overridden the form vs stock registration form

Old:

Email: [email protected]
Username: uniquename
Password: xxxxxxx
Repeat password: xxxxxxx

New:

Email: [email protected]
User_name: duplicatesallowed
Password: xxxxxxx
Repeat password: xxxxxxx

Modify the controller so user.username takes value of user.email (because emails will be unique).

Upvotes: 0

dmnptr
dmnptr

Reputation: 4304

Judging from your comments I believe that you confuse username with first and last name. Your users may have same first and last name, but they must not have the same username. FOSUserBundle uses username/password combination to authenticate users. With some configuration you may allow users to login with email/password, or even use email as username.

In any case username is supposed to stay unique. If you configured FOSUserBundle properly, you should have your own user entity class that extends user entity from the bundle. That is where you add additional fields, such as first and last name, and so on.

Upvotes: 1

Related Questions