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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok I managed to adapt it ;-)
Hope it will help. Here is the Profile Form Type :
And the service (mine is in yml) :
And of course, adding in config.yml :
Thank you again for your example !