有幾種方法可以解決這個問題。我看到你在Yii的論壇貼出所以我想你已經四處搜尋有太多,但如果你沒有:
我所做的是(只對於一個簡單的2步ActiveRecord表單)採取一個單一的行動,並根據按鈕名稱,Yii張貼在表單提交(注:不適用於Ajax提交)的POST名稱分爲條件塊。然後,根據哪個按鈕被擊中,我會渲染正確的表單,並在我的模型上設置正確的方案以進行驗證。
像你一樣隱藏的「step」字段可以起到與檢查submitButton名稱相同的作用。我可能會將「步驟」保存到表單狀態中,而不是添加隱藏字段,但兩者都可以。
有些人使用有狀態的activeForm屬性來保存嚮導中單個步驟的數據,或者您可以使用會話,甚至保存到臨時數據庫表中。在我完全未經測試的例子中,我使用了有狀態表單功能。
這裏是我基本上爲ActiveRecord表單做的一個例子。這正好在 「actionCreate」:
<?php if (isset($_POST['cancel'])) {
$this->redirect(array('home'));
} elseif (isset($_POST['step2'])) {
$this->setPageState('step1',$_POST['Model']); // save step1 into form state
$model=new Model('step1');
$model->attributes = $_POST['Model'];
if($model->validate())
$this->render('form2',array('model'=>$model));
else {
$this->render('form1',array('model'=>$model));
}
} elseif (isset($_POST['finish'])) {
$model=new Model('finish');
$model->attributes = $this->getPageState('step1',array()); //get the info from step 1
$model->attributes = $_POST['Model']; // then the info from step2
if ($model->save())
$this->redirect(array('home'));
else {
$this->render('form2',array('model'=>$model));
} else { // this is the default, first time (step1)
$model=new Model('new');
$this->render('form1',array('model'=>$model));
} ?>
的形式會是這個樣子:
Form1中:
<?php $form=$this->beginWidget('CActiveForm', array(
'enableAjaxValidation'=>false,
'id'=>'model-form',
'stateful'=>true,
));
<!-- form1 fields go here -->
echo CHtml::submitButton("Cancel",array('name'=>'cancel');
echo CHtml::submitButton("On to Step 2 >",array('name'=>'step2');
$this->endWidget(); ?>
表2:
<?php $form=$this->beginWidget('CActiveForm', array(
'enableAjaxValidation'=>false,
'id'=>'model-form',
'stateful'=>true,
));
<!-- form2 fields go here -->
echo CHtml::submitButton("Back to Step 1",array('name'=>'step1');
echo CHtml::submitButton("Finish",array('name'=>'finish');
$this->endWidget(); ?>
我希望這是有幫助!
看看'了CHtml :: statefulForm'和[線程的Yii論壇](http://www.yiiframework.com/forum/index.php?/topic/ 8413-wizard-forms /) – 2010-08-23 20:31:36