Zfcuser添加寄存器字段

时间:2016-02-03 13:25:27

标签: zend-framework2 zend-auth zfcuser

我试图在寄存器中的zfcuser moudle中添加新的寄存器字段。我有问题bcs新字段未在register.phtml

中呈现

我的所作所为:

首先,我在自定义模块中创建新的User实体并扩展\ZfcUser\Entity\User,并添加新属性protected $first_name并添加getter方法。

其次,我在配置中更改了user_entity_class' => 'MyModule\Entity\User

第三,我创建自定义表单类,其中我扩展\ZfcUser\Form\Register,其中我创建了两个方法__constructor($name,RegistrationOptionsInterface $options),第二个init()。这看起来像这样:

// 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'
            ),
        ));
}

我在模块中将其注册为sercice:

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

                return $clientRegisterForm;
            }
        )
    );
}

所以问题是bcs zfcuser对新领域一无所知。循环列表只是默认字段。如何以这种方式通知zfcuser模块关于新字段?

register.phtml

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

1 个答案:

答案 0 :(得分:0)

通常,您首先修改 module.config.php ,以便正确反映您的zfcuser_entity。

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,
    ),
);

然后在你的bootstrap中你需要监听附加到ZfcUser \ Form \ RegisterFilter,ZfcUser \ Form \ Register(都是init)来修改表单。

最后,同样在你的引导程序中,你附加到&#39;注册表&#39; &#39; zfcuser_user_service&#39;上的活动活动经理。

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');*/
        });
    }
}

我在这里完成了一个完整的文章: http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6

如今,我只是将我自己的用户实体,Auth服务与我自己的Login,Reg表单等结合起来。最初我喜欢ZfcUser和BjyAuthorize的组合 - 但是BjyAuthorize + custom是一个更好的享受!

祝你好运。