我正在尝试用 Yii 构建一个多页表单,但我对 PHP 和 Yii 很陌生,我想知道编写多页表单的最佳实践是什么.到目前为止,我打算做的是添加一个名为step"的隐藏字段,其中包含用户在表单中的当前步骤(表单分为 3 个步骤/页面).因此,考虑到这一点,这就是我计划处理用户单击控制器中的上一个/下一个按钮的方式:
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:
我所做的是(仅针对一个简单的 2 步 ActiveRecord 表单)采取了一个单一的行动,并根据按钮名称将其划分为条件块,Yii 在表单提交上发布(注意:不起作用)与 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.
像您这样隐藏的步骤"字段可以起到与检查 submitButton 名称相同的目的.我可能会将步骤"保存到表单状态而不是添加隐藏字段,但两者都可以.
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));
} ?>
表单看起来像这样:
表格 1:
<?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 多页表单向导最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!