1
votes

Commuity,

i use the FosUserBundle and i have a problem with the username.

On my page, you only need a email and a password. => you could use a username! (because that, i couldnt override the setEmail() function in entity with setUsername($email) ^^

If i use this in my configuration, i get the following error.

fos_user:
    registration:
        form:
            validation_groups: [AppRegistration]

Error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username' cannot be null 

What can i do?

Thank you in Advance!

2
Removing the username might cause complications, so I would recommend you overwrite setEmail($email) to also set username in your User-class. This might be redundant, but makes makes sure that each stuff requiring a username still works, e.g. login via username+password. Of course you also have to change the signup form accordingly, so that users don't choose a username other than their email-address, also you might want to change the translation to say email instead of username.dbrumann
Hmm okay. I need a field "eusername" which is the correct, optional username and the username "email" ... Then i must do that. Thanks. Hope there is another way, too. (maybe in future!)Patrick

2 Answers

5
votes
# app/config/security.yml
security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

Change your security.yml to make fosuserbundle accept email as username, from Logging by Username or Email and make sue you overwrite your setUsername method, because username cannot be empty. E.g.

public function setEmail($email){
    parent::setEmail($email);
    parent::setUsername($email);
}
0
votes

You can make the username nullable by using the Doctrine\ORM\Mapping\AttributeOverrides and Doctrine\ORM\Mapping\AttributeOverride annotations above the User entity like this:

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
{
//..

This code will make the username nullable in the database AND not required in the validation rules.

Source: https://openclassrooms.com/forum/sujet/symfony-supprimer-username-fosuserbundle