Zfcuser add register fields

265 views Asked by At

Am trying to add new register fields in zfcuser moudle in register. I have problem bcs new fields not rendered in register.phtml

What i do:

First i create new User entity in my custom module and extend \ZfcUser\Entity\User, and add new properties protected $first_name and put getter method.

Second, i change user_entity_class' => 'MyModule\Entity\User in config.

Third, i create custom form class where i extend \ZfcUser\Form\Register where i create two methods __constructor($name,RegistrationOptionsInterface $options), second init(). THis look like this:

// Module/src/Mymod/Form

class ClientRegisterForm extends \ZfcUser\Form\Register
{
    public function __construct($name, RegistrationOptionsInterface $options)
    {
        parent::__construct($name, $options);
    }

    /**
     * {@inheritDoc}
     */
    public function init(){
        $this->add(array(
            'name' => 'first_name',
            'options' => array(
                'label' => 'First name',
            ),
            'attributes' => array(
                'type' => 'text'
            ),
        ));
}

And i register this like sercice in module:

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'clientRegisterForm' => function($sm) {
                $clientRegisterForm = new ClientRegisterForm(null, array());

                return $clientRegisterForm;
            }
        )
    );
}

So problem is bcs zfcuser dont know nothing about new field. Loop list just default fields. How to notify zfcuser module about new field in this way?

register.phtml

<?php foreach ($form as $element): ?>
<?php echo $this->formInput($element) . $this->formElementErrors($element) ?>
<?php endforech; ?>
1

There are 1 answers

0
Saeven On

Typically you'll first modify your module.config.php so that it reflects your zfcuser_entity properly.

return array(
    'doctrine' => array(
        'driver' => array(
            // overriding zfc-user-doctrine-orm's config
            'zfcuser_entity' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'paths' => __DIR__ . '/../src/FooUser/Entity',
            ),

            'orm_default' => array(
                'drivers' => array(
                    'FooUser\Entity' => 'zfcuser_entity',
                ),
            ),
        ),
    ),

    'zfcuser' => array(
        // telling ZfcUser to use our own class
        'user_entity_class'       => 'FooUser\Entity\User',
        // telling ZfcUserDoctrineORM to skip the entities it defines
        'enable_default_entities' => false,
    ),
);

Then in your bootstrap you need to listen to attach to ZfcUser\Form\RegisterFilter, ZfcUser\Form\Register (both init) to modify the form.

Lastly, also in your bootstrap, you attach to the 'register' event on the 'zfcuser_user_service' event manager.

namespace FooUser;

use Zend\Mvc\MvcEvent;

class Module {

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';

    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function onBootstrap( MVCEvent $e )
    {
        $eventManager = $e->getApplication()->getEventManager();
        $em           = $eventManager->getSharedManager();
        $em->attach(
            'ZfcUser\Form\RegisterFilter',
            'init',
            function( $e )
            {
                $filter = $e->getTarget();
                // do your form filtering here            
            }
        );

        // custom form fields

        $em->attach(
            'ZfcUser\Form\Register',
            'init',
            function($e)
            {
                /* @var $form \ZfcUser\Form\Register */
                $form = $e->getTarget();
                $form->add(
                    array(
                        'name' => 'username',
                        'options' => array(
                            'label' => 'Username',
                        ),
                        'attributes' => array(
                            'type'  => 'text',
                        ),
                    )
                );

                $form->add(
                    array(
                        'name' => 'favorite_ice_cream',
                        'options' => array(
                            'label' => 'Favorite Ice Cream',
                        ),
                        'attributes' => array(
                            'type'  => 'text',
                        ),
                    )
                );
            }
        );

        // here's the storage bit

        $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_ice_cream')->getValue() );
        });

        // you can even do stuff after it stores           
        $zfcServiceEvents->attach('register.post', function($e) {
            /*$user = $e->getParam('user');*/
        });
    }
}

I've got a full on post that walks through the minutia here: http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6

Nowadays, I simply roll my own User entities, Auth service in conjunction with my own Login, Reg forms and so forth. Originally I liked the combination of ZfcUser and BjyAuthorize -- but BjyAuthorize + custom has been a better treat!

Good luck man.