Yii多页表单向导最佳实践 [英] Yii multi page form wizard best practice

查看:93
本文介绍了Yii多页表单向导最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Yii构建多页表单,但是对于PHP和Yii来说还很陌生,我想知道编写多页表单的最佳实践是什么.到目前为止,我打算做的是添加一个名为"step"的隐藏字段,该字段包含用户在表单中所处的当前步骤(表单分为3个步骤/页面).因此,考虑到这一点,这就是我打算如何处理用户单击Controller中的上一个/下一个按钮的方法:

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 planning to do is to add a hidden field named 'step' which contains the current step the user is on in the form (the form is broken into 3 steps/pages). So with that in mind, this is how I plan to handle the user clicking on previous/next buttons in the Controller:

public function actionCreate()
 {
  $userModel = new User;

  $data['activityModel'] = $activityModel; 
  $data['userModel'] = $userModel; 

  if (!empty($_POST['step']))
  {
   switch $_POST['step']:
    case '1':
     $this->render('create_step1', $data);
     break;

    case '2':
     $this->render('create_step2', $data);
     break;

  }else
  {
   $this->render('create_step1', $data);
  }
 }

这种方法有意义吗?还是我脱离了基础,并且在Yii/PHP中有更好,更优化的方法来做到这一点?

Does this approach make sense? Or am I way off base and there is a much better and more optimized way of doing this in Yii/PHP?

谢谢!

推荐答案

有两种方法可以解决此问题.我看到您在Yii论坛上发帖,所以我假设您也在附近搜索过,但如果您没有这样做:

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/

我所做的是(仅针对简单的两步ActiveRecord表单)采取了一项操作,并根据按钮名称将其分为条件块,Yii在表单上进行了POST提交(注意:不起作用)与ajax提交).然后,根据所单击的按钮,我渲染正确的表单并在模型上设置正确的场景以进行验证.

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.

像您一样的隐藏的步骤"字段可以起到与检查commitButton名称相同的作用.我也许可以将步骤"保存到表单状态中,而不是添加一个隐藏字段,但是两者都可以.

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.

某些人使用有状态的activeForm属性从向导的单个步骤中保存数据,或者您可以使用会话,甚至保存到临时数据库表中.在下面的我完全未经测试的示例中,我使用的是有状态表单功能.

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.

这是我基本上为ActiveRecord表单所做的一个示例.这放在"actionCreate"中:

Here is an example of what I basically did for an ActiveRecord form. This goes in the "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(); ?>

我希望这会有所帮助!

这篇关于Yii多页表单向导最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆