The answer is very simple - you need to do nothing to update the User object in the registration form after updating the User in the event listener for the FOSUserEvents::REGISTRATION_INITIALIZE event.
Let me explain. FOSUserEvents::REGISTRATION_INITIALIZE sent to RegistrationController by:
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));
And before this submission, a new User is created in the code ( https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php#L43 ):
$user = $userManager->createUser(); $user->setEnabled(true); $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));
When sending by default, PHP call_user_func ( http://php.net/manual/en/function.call-user-func.php ) is called with an embedded event name (function in a specific object) and an Event object. After that, the event listener has the ability to modify the attached Event object - especially the event property.
In your case, your event handler will change the User property with:
$user = $event->getUser(); $user->addRole( 'ROLE_USER' );
Therefore, in fact, you do not need to do anything to submit the changed user object back to the registration form.
source share