1
votes

I have modified ZfcUser register form in my module (Module.php) with two new fields "firstname" and "lastname" like this:

public function onBootstrap($e)
{
    $events           = $e->getApplication()->getEventManager()->getSharedManager();
    $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();

    $events->attach('ZfcUser\Form\Register', 'init', function($e) {
        $form = $e->getTarget();
        $form->add(array(
                'name' => 'firstname',
                'type' => 'Text',
                'options' => array(
                        'label' => 'First name: ',
                ),
        ));
        $form->add(array(
                'name' => 'lastname',
                'type' => 'Text',
                'options' => array(
                        'label' => 'Last name: ',
                ),
        ));
    });

    $events->attach('ZfcUser\Form\RegisterFilter', 'init', function($e) {
        $filter = $e->getTarget();
        $filter->add(array(
                'name'       => 'firstname',
                'required'   => true,
                'validators' => array(
                        array(
                                'name'    => 'StringLength',
                                'options' => array(
                                        'min' => 3,
                                        'max' => 255,
                                ),
                        ),
                ),
        ));
        $filter->add(array(
                'name'       => 'lastname',
                'required'   => true,
                'validators' => array(
                        array(
                                'name'    => 'StringLength',
                                'options' => array(
                                        'min' => 3,
                                        'max' => 255,
                                ),
                        ),
                ),
        ));
    });

    $zfcServiceEvents->attach('register', function($e) {
        $user = $e->getParam('user');  // User account object
        $form = $e->getParam('form');  // Form object
        //var_dump($form->get('firstname')->getValue()); die;
        //var_dump($user); die;
    });

    $zfcServiceEvents->attach('register.post', function($e) {
        $user = $e->getParam('user');
    });
}

Registration form looks ok. I can see to additional fields and validation is working fine. Problem is that I can't set this two new fields in user object:

$user->setFirstname($form->get('firstname')->getValue());

It is saying that this property doesn't exists. Can you explain what I'm doing wrong please?

1

1 Answers

4
votes

Ok is solve it. In my module.config.php I had to add this:

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

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

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

And then extend ZfcUser\Entity\User with mine:

namespace User\Entity;
use ZfcUser\Entity\User as ZfcUser;
class User extends ZfcUser
{
    /**
     * @var string
     */
    protected $firstname;

    /**
     * @var string
     */
    protected $lastname;

    /**
     * Get firstname.
     *
     * @return string
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set firstname.
     *
     * @param string $firstname
     * @return UserInterface
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
        return $this;
    }

    /**
     * Get lastname.
     *
     * @return string
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * Set lastname.
     *
     * @param string $lastname
     * @return UserInterface
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
        return $this;
    }
}

All done thx to this tutorial http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6