gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
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.
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)
.
... 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.
The only other thing we need to do is remove the username
field from the registration
form. How? Easy: in RegistrationFormType
, add ->remove('username')
.
... 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.
There is a little flaw in it. If You remove username, on login page You should change label name to "E-mail", so people would know what should they input there. The simplest way form my point of view is to generate translation (app->Resources-> make file FOSUserBundle.lang.yml (lang is Your language eg. en) and override existing translation (security: login: username: and change text to E-mail which is our user name :)
Nice catch! and I like your solution, but if you manually created the login form, then you can just tweak that label in the template :)
Cheers!
// 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
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"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"
}
}
Very useful, thank you for this!