2014-01-16 83 views
1

我正在使用zfcUser進行註冊和身份驗證,並且我有一些問題。用戶可以通過兩種類型的配置文件進行註冊,並且表單是不同的,所以問題是,在zfcUser中使用少量註冊表單,在一個頁面上使用製表符的最佳做法是什麼。zfcUser兩個註冊表單?

回答

1

我能想到幾個解決方案。 這是我認爲最適合您的用途。這是一些代碼。

畢竟,你必須考慮數據庫,可能你會有一個表具有不同的字段,或者一個公用表和不同的表具有額外的字段取決於用戶類型。解決方案是一樣的。

首先,如您所知,有一項檢索註冊表單的服務。你可以使用註冊事件回調來修改它們,但是,由於你需要不同的表單,而且它們都不是標準表單,所以我認爲最好的做法是爲這兩種新表單創建兩個新的服務。

對於這一點,在getServiceConfig()功能的模塊中,創建此兩項服務,即回覆zfcuser_register_form服務,但添加和刪除字段和輸入濾波器領域

<? 
public function getServiceConfig() 
{ 
    return array(

     'factories' => array(

      'zfcuser_register_form_usertype1' => function ($sm) { 
       $options = $sm->get('zfcuser_module_options'); 
       $form = new Form\Register(null, $options); 

       ///for type: 
       $this->add(array(
        'name' => 'usertype', 
        'attributes' => array(
         'type' => 'hidden', 
         'value' => '1', 
        ), 
       )); 

       /* 
       * Add o remove fields to the form 
       */ 


       $form->setInputFilter(new Form\RegisterFilter(
        new Validator\NoRecordExists(array(
         'mapper' => $sm->get('zfcuser_user_mapper'), 
         'key' => 'email' 
        )), 
        new Validator\NoRecordExists(array(
         'mapper' => $sm->get('zfcuser_user_mapper'), 
         'key' => 'username' 
        )), 
        $options 
       )); 


       /* 
       * Add o remove fields to the input filter 
       */ 
       return $form; 
      }, 

      //////////////////////////////////////////////////////////// 


      'zfcuser_register_form_usertype2' => function ($sm) { 
       $options = $sm->get('zfcuser_module_options'); 
       $form = new Form\Register(null, $options); 

       ///for type: 
       $this->add(array(
        'name' => 'usertype', 
        'attributes' => array(
         'type' => 'hidden', 
         'value' => '2', 
        ), 
       )); 

       /* 
       * Add o remove fields to the form 
       */ 


       $form->setInputFilter(new Form\RegisterFilter(
        new Validator\NoRecordExists(array(
         'mapper' => $sm->get('zfcuser_user_mapper'), 
         'key' => 'email' 
        )), 
        new Validator\NoRecordExists(array(
         'mapper' => $sm->get('zfcuser_user_mapper'), 
         'key' => 'username' 
        )), 
        $options 
       )); 


       /* 
       * Add o remove fields to the input filter 
       */ 
       return $form; 
      }, 




     ), 
    ); 

} 

有關如何添加Ø指南刪除字段去the official manual page on forms

然後,在CONTROLER /動作,將顯示報名表(它應該是你自己的,路線,而不是標準zfuser一個)

public function registerAction() 
{ 
    //get the first form 
    $form1 = $this->getServiceLocator()->get ('zfcuser_register_form_usertype1'); 

    //set the action to the zfuser registation route 
    $form1->setAttribute('action', $this->url('zfcuser/register')); 
    $form1->setAttribute('method', 'post'); 


    //the same for the second form 
    $form2 = $this->getServiceLocator()->get ('zfcuser_register_form_usertype2'); 
    $form2->setAttribute('action', $this->url('zfcuser/register')); 
    $form2->setAttribute('method', 'post'); 

    //send both form to the view 
    return array('form1' => $form1,'form2' => $form2); 
} 

然後視圖,只需檢索的形式,使它們,就像使用任何形式的(如果您有任何疑問,請訪問上面鏈接手冊中,還包含如何呈現形式的信息)

<?php 

/* 
* html for tabs 
*/ 

$form = $this->form1; 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formHidden($form->get('usertype')); 
echo $this->formRow($form->get('field1')); 
echo $this->formRow($form->get('field2')); 
echo $this->formSubmit($form->get('submit')); 
echo $this->form()->closeTag(); 

/* 
* other html for tabs 
*/ 

$form2 = $this->form2; 
$form2->prepare(); 

echo $this->form()->openTag($form2); 

echo $this->formHidden($form->get('usertype')); 
echo $this->formRow($form2->get('field1')); 
echo $this->formRow($form2->get('field2')); 
echo $this->formSubmit($form2->get('submit')); 
echo $this->form()->closeTag(); 


/* 
* other html for tabs 
*/ 

現在,你有你的表格。唯一剩下的就是處理註冊。正如你可能知道,zfcuser有一個事件管理器,alows我們計劃操作的一些事件,例如,when a new account is created

對於這一點,你的模塊中,在onBootstrap功能,您

<? 
/** 
* executes on boostrap 
* 
* @param \Zend\Mvc\MvcEvent $e   
* @return null 
*/ 
public function onBootstrap(MvcEvent $e) { 


    //retrieve the zfcuser event manager 
    $sm = $e->getApplication()->getServiceManager(); 
    $zfcServiceEvents = $sm->get('zfcuser_user_service')->getEventManager(); 

    // add a callback to Store the field when the form is submited 
    $zfcServiceEvents->attach('register', function ($e) use($sm) 
    { 
     $form = $e->getParam('form');//here you have the submited form 
     $user = $e->getParam('user');//here you have the already created standard user entity 


     $type=$form->get("usertype")->getValue();//retrieve the user type 
     //with this, you can decide what other fields to retrieve, 
     //and how to asign it to the entity or to other related entities: 

     if ($type==1) { 
       //... 
     } 
     else { 
       //... 

     } 


    }); 
} 
+0

很好的解決方案,但我做了我自己的模塊,它會更容易 – Cawa