Skip to content

How to store custom form values into your user entity at registration

Saeven edited this page Mar 26, 2013 · 4 revisions

Ref, using ZF 2.1.4 at the time this was written

A bit of a follow up to: https://github.com/ZF-Commons/ZfcUser/wiki/How-to-embed-the-login-form-on-another-page

In your bootstrap event (hopefully in a custom Module for your user entities and roles and so forth), add this block to your onBootstrap code:

public function onBootstrap( MVCEvent $e )
{
    $eventManager = $e->getApplication()->getEventManager();
    $em           = $eventManager->getSharedManager();

    // ...
 
    $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
    $zfcServiceEvents->attach('register', function($e) {
        $form = $e->getParam('form');
        $user = $e->getParam('user');
            
        /* @var $user \FooUser\Entity\User */
        $user->setUsername( $form->get('username')->getValue() );
        $user->setFavoriteIceCream( $form->get('favorite_icecream')->getValue() );
    });
}

Clone this wiki locally