Yii multi page form wizard best practice

前端 未结 2 670
借酒劲吻你
借酒劲吻你 2020-12-12 17:55

I am trying to build a multi-page form with Yii, but am quite new to PHP and Yii and am wondering what the best practice is for writing a multi page form. So far, what I am

2条回答
  •  轮回少年
    2020-12-12 17:58

    There are a couple of ways to approach this. I see you posted in the Yii forum so I assume you've searched around there too but in case you haven't:

    • http://www.yiiframework.com/forum/index.php?/topic/6203-stateful-forms-and-wizards-howto/
    • http://www.yiiframework.com/forum/index.php?/topic/8413-wizard-forms/

    What I have done is (just for a simple 2-step ActiveRecord form) taken a single action and divided it up into conditional blocks based on the button name, which Yii POSTs on a form submit (note: doesn't work with ajax submits). Then, depending on which button was hit I render the correct form and set the correct scenario on my model for validation purposes.

    A hidden "step" field like you have could serve the same purpose as the checking the submitButton name. I would perhaps save the "step" into the form state instead of adding a hidden field though, but either would be fine.

    Some people use the stateful activeForm attribute to save the data from a single step in the wizard, or you can use the session, or even save to a temp database table. In my completely untested example below I am using a the stateful form functionality.

    Here is an example of what I basically did for an ActiveRecord form. This goes in the "actionCreate":

    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));
    } ?>
    

    The forms would look something like this:

    Form1:

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

    Form 2:

    beginWidget('CActiveForm', array(
        'enableAjaxValidation'=>false,
        'id'=>'model-form',
        'stateful'=>true,
    ));
    
    echo CHtml::submitButton("Back to Step 1",array('name'=>'step1');
    echo CHtml::submitButton("Finish",array('name'=>'finish');
    $this->endWidget(); ?>
    

    I hope that is helpful!

提交回复
热议问题