Created
May 7, 2014 11:07
-
-
Save nchervyakov/c013aa2eaf4da72150f6 to your computer and use it in GitHub Desktop.
Symfony2 FOSUserBundle. Registration using just email.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyProject\UserBundle\Form\Type; | |
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType; | |
use FOS\UserBundle\Model\UserManager; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use MyProject\UserBundle\Entity\User; | |
class RegistrationFormType extends BaseType | |
{ | |
protected $userManager; | |
// Add constructor with UserManager | |
public function __construct($class, UserManager $userManager) | |
{ | |
parent::__construct($class); | |
$this->userManager = $userManager; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
parent::buildForm($builder, $options); | |
$builder | |
->add('firstName') | |
->add('lastName') | |
->remove('username'); | |
// Add hook on submitted data in order to copy email into username | |
$um = $this->userManager; | |
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($um) { | |
$user = $event->getData(); | |
/** @var User $user */ | |
if ($user) { | |
// Set username like email and canonicalize both fields | |
$user->setUsername($user->getEmail()); | |
$um->updateCanonicalFields($user); | |
} | |
}); | |
} | |
public function getName() | |
{ | |
return 'mp_user_registration'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<parameters> | |
<parameter key="mp_user.registration.form.class">MyProject\UserBundle\Form\Type\RegistrationFormType</parameter> | |
</parameters> | |
<services> | |
<service id="mp_user.registration.form.type" class="%mp_user.registration.form.class%"> | |
<tag name="form.type" alias="mp_user_registration" /> | |
<argument>%fos_user.model.user.class%</argument> | |
<!-- Add UserManager here --> | |
<argument type="service" id="fos_user.user_manager" /> | |
</service> | |
</services> | |
</container> |
That's working perfectly for me. Thank you very much. Would you by any chance have the same simple adaptation for the ProfileFormType?
Ok I managed to adapt it ;-)
Hope it will help. Here is the Profile Form Type :
<?php
namespace Acme\UserBundle\Form\Type;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
use FOS\UserBundle\Model\UserManager;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword as OldUserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
//use Acme\UserBundle\Entity\User;
class ProfileFormType extends BaseType
{
protected $userManager;
// Add constructor with UserManager
public function __construct($class, UserManager $userManager)
{
parent::__construct($class);
$this->userManager = $userManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (class_exists('Symfony\Component\Security\Core\Validator\Constraints\UserPassword')) {
$constraint = new UserPassword();
} else {
// Symfony 2.1 support with the old constraint class
$constraint = new OldUserPassword();
}
$this->buildUserForm($builder, $options);
$builder
->remove('username')
->add('current_password', 'password', array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => $constraint,
));
// Add hook on submitted data in order to copy email into username
$um = $this->userManager;
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($um) {
$user = $event->getData();
/** @var User $user */
if ($user) {
// Set username like email and canonicalize both fields
$user->setUsername($user->getEmail());
$um->updateCanonicalFields($user);
}
});
}
public function getName()
{
return 'acme_user_profile';
}
}
And the service (mine is in yml) :
acme_user.profile.form.type:
class: Acme\UserBundle\Form\Type\ProfileFormType
arguments:
- %fos_user.model.user.class%
- @fos_user.user_manager
tags:
- { name: form.type, alias: acme_user_profile }
And of course, adding in config.yml :
fos_user:
profile:
form:
type: acme_user_profile
Thank you again for your example !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here I have added my method of removing username from registration form. It works quite well.
Why I didn't implement my own UserInterface class: