TRACK

Symfony 3 >

My Users don't have a Username!

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Okay: a challenge! In some apps... you don't even need a username - you register and login entirely with your email.

But... with FOSUserBundle, we always have a username field. So, are we stuck?

Nope! It is true that you can't remove the username field from your user table. But, we can remove it from everywhere else.

Auto-Setting the Username Field

Start in the User class. I'll go to the Code->Generate menu - or Command+N on a Mac - go to "Override Methods" and choose setEmail(). Before the parent call, add $this->setUsername($email).

<?php
... lines 2 - 11
class User extends BaseUser
{
... lines 14 - 40
/**
* Overridden so that username is now optional
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->setUsername($email);
return parent::setEmail($email);
}
}

This is big! Thanks to this, we no longer need to worry about ever setting the username field. In the database, username will always equal the email... which is definitely redundant and unnecessary, but in practice, it works fine.

Removing the Username Field from the Form

The only other thing we need to do is remove the username field from the registration form. How? Easy: in RegistrationFormType, add ->remove('username').

<?php
... lines 2 - 8
class RegistrationFormType extends AbstractType
... line 10
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName')
->remove('username');
}
... lines 17 - 21
}

Then, in the template, remove its form_row().

... lines 1 - 2
<h1>Register Aquanaut!</h1>
... line 4
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}
{{ form_row(form.email) }}
{{ form_row(form.firstName) }}
{{ form_row(form.plainPassword) }}
... lines 9 - 11
{{ form_end(form) }}

And yea, that's it! Refresh! No more username! Register as aquanaut4@gmail.com and submit. Check it out in the database:

php bin/console doctrine:query:sql 'SELECT * FROM user'

There it is! The username is now equal to the email.

Oh, and if you're using the profile edit page from the bundle, you'll also want to remove the username field from the ProfileFormType form. It's done in exactly the same way.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.0
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
        "symfony/swiftmailer-bundle": "^2.3", // v2.5.4
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.3.0
        "sensio/distribution-bundle": "^5.0", // v5.0.18
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.25
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.5.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "friendsofsymfony/user-bundle": "^2.0" // v2.0.0
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.4
        "symfony/phpunit-bridge": "^3.0", // v3.2.7
        "nelmio/alice": "^2.1", // v2.3.1
        "doctrine/doctrine-fixtures-bundle": "^2.3", // v2.4.1
        "symfony/web-server-bundle": "^3.3"
    }
}