[英]Create Register User in Yii Framework
我想分三步創建注冊頁面:
Step1:基本信息(ext:example.site/register/step1)
步驟 2:個人資料信息(分機:example.site/register/step2)
Step3:完成(ext:example.site/register/step3)
看起來像:
有人可以幫助我嗎?
要實現這樣的目標,您有兩種選擇:
以下代碼未經測試,但應該讓您了解如何完成。 讓我們從模型開始。 該模型包含所有頁面的數據。 在兩個輸入頁面中僅使用模型使事情變得更容易。
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
}
}
現在到控制器。 根據您上面的網址,它應該是這樣的:
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));
}
}
}
觀點很簡單。 您必須記住,您必須在第二頁上添加隱藏字段以保留步驟 1 的數據。步驟一僅包含名字、姓氏和電子郵件的輸入字段。 第 2 步包含密碼、性別和年齡的輸入字段以及名字、姓氏和電子郵件的隱藏字段。 成功視圖根本不包含輸入字段,因為注冊過程到那時就完成了。
除了這個自定義解決方案,還有一個向導擴展可以派上用場。 您可以在這里找到它: http ://www.yiiframework.com/extension/wizard-behavior/ 此外,這里已經回答了類似的解決方案: https : //stackoverflow.com/a/3551704/3402681
我為您提供了上面的答案,讓您對如何處理這個問題有一個大致的了解。 對此沒有“正確”的解決方案。 你必須找到最適合你的那個。 希望我能幫上忙!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.