首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zfcuser添加寄存器字段

Zfcuser添加寄存器字段
EN

Stack Overflow用户
提问于 2016-02-03 13:25:26
回答 1查看 249关注 0票数 0

我试图在寄存器中的zfcuser鼠标中添加新的寄存器字段。我有问题bcs新字段没有在register.phtml中呈现

我所做的:

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

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

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

代码语言:javascript
复制
// 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一样注册这个:

代码语言:javascript
复制
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'clientRegisterForm' => function($sm) {
                $clientRegisterForm = new ClientRegisterForm(null, array());

                return $clientRegisterForm;
            }
        )
    );
}

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

register.phtml

代码语言:javascript
复制
<?php foreach ($form as $element): ?>
<?php echo $this->formInput($element) . $this->formElementErrors($element) ?>
<?php endforech; ?>
EN

回答 1

Stack Overflow用户

发布于 2016-02-03 20:52:01

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

代码语言:javascript
复制
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,
    ),
);

然后,在引导过程中,您需要侦听连接到ZfcUser\ form \RegisterFilter、ZfcUser\form\寄存器(都是init)来修改表单。

最后,在引导过程中,还可以附加到“zfcuser_user_service”事件管理器上的“寄存器”事件。

代码语言:javascript
复制
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服务与我自己的登录、Reg表单等结合起来。最初,我喜欢ZfcUser和BjyAuthorize的结合--但是BjyAuthorize +自定义是一个更好的选择!

祝你好运,伙计。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35178335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档