2016-02-03 55 views
0

我试图在寄存器中的zfcuser模块中添加新的寄存器字段。我有问题BCS新领域中register.phtmlZfcuser添加寄存器字段

不会呈现我该怎么办:

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

其次,我在config中更改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; ?> 

回答

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

然后在你的引导你需要听重视ZfcUser \表格\ RegisterFilter,ZfcUser \表格\注册(包括初始化)修改窗体。

最后,同样在您的引导程序中,您将附加到'zfcuser_user_service'事件管理器上的'register'事件。

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

现在,我只是推出自己的用户实体,验证服务结合我自己的登录,注册形式和等等。最初我喜欢ZfcUser和BjyAuthorize的组合 - 但BjyAuthorize +自定义更好!

祝你好运。