我希望在3个步骤中创建注册页面:
Step1:基本信息(分机:example.site/register/step1)
Step2:Profile Infomation(ext:example.site/register/step2)
Step3:Done(ext:example.site/register/step3)
喜欢:
有人可以帮助我吗?
答案 0 :(得分:0)
要实现这样的目标,您有两种选择:
以下代码未经过测试,但应该让您了解如何完成此操作。让我们从模型开始。 该模型包含所有页面的数据。仅使用两个输入页面上的模型可以更轻松。
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的数据。第一步只包含firstname,lastname和email的输入字段。第2步包含密码,性别和年龄的输入字段以及firstname,lastname和email的隐藏字段。成功视图根本不包含输入字段,因为注册过程在那时完成。
除了这个自定义解决方案之外,还有一个向导扩展,可以派上用场。你可以在这里找到它:http://www.yiiframework.com/extension/wizard-behavior/ 此外,这里已经回答了一个类似的解决方案:https://stackoverflow.com/a/3551704/3402681
我为您提供了上述答案,为您提供有关如何处理此问题的一般见解。对此没有“正确”的解决方案。你必须找到最适合你的那个。希望我能帮忙!