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.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe
But then I am stuck with two constraint violations.
Problem is if the user leaves the email address blank, I get two constraint violations:
- Please enter a username
- Please enter an email
Is there a way to disable validation for a given field, or a better way to remove a field from the form altogether?
A complete overview of what needs to be done
Here is a complete overview of what needs to be done. I have listed the different sources found here and there at the end of this post.
1. Override setter in
Acme\UserBundle\Entity\User
2. Remove the username field from your form type
(in both
RegistrationFormType
andProfileFormType
)3. Validation constraints
As shown by @nurikabe, we have to get rid of the validation constraints provided by
FOSUserBundle
and create our own. This means that we will have to recreate all the constraints that were previously created inFOSUserBundle
and remove the ones that concern theusername
field. The new validation groups that we will be creating areAcmeRegistration
andAcmeProfile
. We are therefore completely overriding the ones provided by theFOSUserBundle
.3.a. Update config file in
Acme\UserBundle\Resources\config\config.yml
3.b. Create Validation file
Acme\UserBundle\Resources\config\validation.yml
That's the long bit:
4. End
That's it! You should be good to go!
Documents used for this post:
As Michael points out, this can be solved with a custom validation group. For example:
Then in your entity (as defined by
user_class: App\UserBundle\Entity\User
) you can use the AppRegistration group: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.
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':
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.
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.
This way, the validator wont trigger any violation. But don't forget to set the email to the username as posted by Patt.
You may have to check other files for references to User:username and change accordingly.
If none of them works, a quick and dirty solution would be
I was able to do this by overriding both the registration and profile form type detailed here and removing the username field
Along with overriding the setEmail method in my concrete user class: