2014-04-04 133 views
-1

創建註冊用戶,我想在3步創建註冊頁面:在Yii框架

第一步:基本信息來源(分機:example.site/register/step1)

第二步:簡介信息來源(分機:例子。網站/註冊/ step2的)

第三步:完成(分機:example.site/register/step3)

looke像:enter image description here

有人能夠幫助我?

+0

你有一些代碼嗎? – Dinistro

回答

0

爲了實現這樣的事情,你有兩個選擇:

  1. 做沒有重新加載使用JavaScript來顯示和隱藏相關領域的頁面。當你爲你的每個gui原型聲明不同的url時,我認爲這不是你想要的。
  2. 使用模型和POST數據將數據從一個頁面發送到另一個頁面。我現在將詳細解釋這種方式,因爲這似乎是您想要的解決方案。實際上,如果您執行函數來檢查是否有足夠的數據來呈現下一頁(請參見下面的模型),則可以通過單個操作完成此操作。

下面的代碼沒有經過測試,但應該給你一個它如何完成的想法。讓我們從模型開始。 該模型包含所有頁面的數據。在兩個輸入頁面中只使用模型可以讓事情變得更輕鬆。

class Registration extends CFormModel { 
    public $firstName; 
    public $lastName; 
    //... 
    public $gender; 
    public $age; 
    //... 

    public function rules() 
    { 
     //add all your field-rules here... 
    } 

    //further functions like attributeLabels(), etc. as needed 

    /** 
    * Returns the fullname as needed on page two 
    */ 
    public function getFirstname() 
    { 
     return $this->firstName . ' ' . $this->lastName; 
    } 

    public function hasValidStepOneData() 
    { 
     //validate the fields you need to continue to step two and return a boolean value. 
     //yii provides this functionality via the validate-function having the abiliy to only 
     //only validate certain fields at once. If one of them fails return false, otherwise true. 

     //see here: http://www.yiiframework.com/doc/api/1.1/CModel#validate-detail 
    } 

    public function hasValidStepTwoData() 
    { 
     //same as above but with values needed for completing step 2 
     //return boolean 
    } 
} 

現在控制器。根據你的網址fforbove它應該是這樣的:

class RegistrationController extends CController { 
    public function actionRegSteps() 
    { 
     $model = new Registration(); 

     //get the values if there are any 
     if (isset($_POST['Registration']) { 
      $model->attributes = $_POST['Registration']; 
     } 

     //decide what to do depending on the data available  
     if ($model->hasValidStepOneData() && $model->hasValidStepTwoData()) { 
      //all data needed is present...save the data and redirect to success-page 
      //TODO: save data here... 
      $model->unsetAttributes(); 
      $this->render('success'); 
     } else if ($model->hasValidStepOneData()) { 
      //step one is complete...show step2 
      $this->render('reg_step2', array('model'=>$model)); 
     } else { 
      //model is still empty...show step 1 
      $this->render('reg_step1', array('model'=>$model)); 
     } 
    } 
} 

意見很簡單。您必須記住,您必須在第2頁上添加隱藏字段以保留第1步的數據。第1步僅包含名字,姓氏和電子郵件的輸入字段。步驟2包含密碼,性別和年齡的輸入字段以及名字,姓氏和電子郵件的隱藏字段。成功視圖完全不包含任何輸入字段,因爲當時註冊過程已完成。

除了這個自定義解決方案之外,還有一個嚮導擴展可以派上用場。你可以在這裏找到它:http://www.yiiframework.com/extension/wizard-behavior/ 另外一個類似的解決方案已經在這裏找到答案:https://stackoverflow.com/a/3551704/3402681

我只要你的答案上面給你的依據是你如何處理這個一般見識。沒有「正確的」解決方案。你必須找到最適合你的那個。希望我能幫忙!