yii_multi_page_form_wizard

Yii multi page form wizard

Controller

<?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->renderPartial('form2',array('model'=>$model));
  else 
    $this->renderPartial('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->renderPartial('form2',array('model'=>$model));
 
} else { // this is the default, first time (step1)
 
  $model=new Model('new');
  $this->renderPartial('form1',array('model'=>$model));
 
} 
?>

Form 1:

<?php $form=$this->beginWidget('CActiveForm', array(
    'enableAjaxValidation'=>false,
    'id'=>'model-form',
    'stateful'=>true,
));?>
<!-- form1 fields go here -->
<?php
echo CHtml::submitButton("Cancel",array('name'=>'cancel'));
echo CHtml::submitButton("On to Step 2 >",array('name'=>'step2'));
$this->endWidget(); ?>

Form 2:

<?php $form=$this->beginWidget('CActiveForm', array(
    'enableAjaxValidation'=>false,
    'id'=>'model-form',
    'stateful'=>true,
));?>
<!-- form2 fields go here -->
<?php
echo CHtml::submitButton("Back to Step 1",array('name'=>'step1'));
echo CHtml::submitButton("Finish",array('name'=>'finish'));
$this->endWidget(); ?>
  • yii_multi_page_form_wizard.txt
  • Последнее изменение: 2012/05/23 07:39
  • (внешнее изменение)