• <i id='OaD2P'><tr id='OaD2P'><dt id='OaD2P'><q id='OaD2P'><span id='OaD2P'><b id='OaD2P'><form id='OaD2P'><ins id='OaD2P'></ins><ul id='OaD2P'></ul><sub id='OaD2P'></sub></form><legend id='OaD2P'></legend><bdo id='OaD2P'><pre id='OaD2P'><center id='OaD2P'></center></pre></bdo></b><th id='OaD2P'></th></span></q></dt></tr></i><div id='OaD2P'><tfoot id='OaD2P'></tfoot><dl id='OaD2P'><fieldset id='OaD2P'></fieldset></dl></div>
    1. <tfoot id='OaD2P'></tfoot>
      • <bdo id='OaD2P'></bdo><ul id='OaD2P'></ul>
      <legend id='OaD2P'><style id='OaD2P'><dir id='OaD2P'><q id='OaD2P'></q></dir></style></legend>

      <small id='OaD2P'></small><noframes id='OaD2P'>

        Yii 多页表单向导最佳实践

        Yii multi page form wizard best practice(Yii 多页表单向导最佳实践)
            <tbody id='xbE6J'></tbody>
              <bdo id='xbE6J'></bdo><ul id='xbE6J'></ul>

            • <tfoot id='xbE6J'></tfoot>

              <small id='xbE6J'></small><noframes id='xbE6J'>

                <legend id='xbE6J'><style id='xbE6J'><dir id='xbE6J'><q id='xbE6J'></q></dir></style></legend>
                <i id='xbE6J'><tr id='xbE6J'><dt id='xbE6J'><q id='xbE6J'><span id='xbE6J'><b id='xbE6J'><form id='xbE6J'><ins id='xbE6J'></ins><ul id='xbE6J'></ul><sub id='xbE6J'></sub></form><legend id='xbE6J'></legend><bdo id='xbE6J'><pre id='xbE6J'><center id='xbE6J'></center></pre></bdo></b><th id='xbE6J'></th></span></q></dt></tr></i><div id='xbE6J'><tfoot id='xbE6J'></tfoot><dl id='xbE6J'><fieldset id='xbE6J'></fieldset></dl></div>
                  本文介绍了Yii 多页表单向导最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试用 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:

                  • 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/

                  我所做的是(仅针对一个简单的 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 多页表单向导最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                1. <legend id='CW7DC'><style id='CW7DC'><dir id='CW7DC'><q id='CW7DC'></q></dir></style></legend>
                2. <i id='CW7DC'><tr id='CW7DC'><dt id='CW7DC'><q id='CW7DC'><span id='CW7DC'><b id='CW7DC'><form id='CW7DC'><ins id='CW7DC'></ins><ul id='CW7DC'></ul><sub id='CW7DC'></sub></form><legend id='CW7DC'></legend><bdo id='CW7DC'><pre id='CW7DC'><center id='CW7DC'></center></pre></bdo></b><th id='CW7DC'></th></span></q></dt></tr></i><div id='CW7DC'><tfoot id='CW7DC'></tfoot><dl id='CW7DC'><fieldset id='CW7DC'></fieldset></dl></div>

                    • <bdo id='CW7DC'></bdo><ul id='CW7DC'></ul>

                          <tfoot id='CW7DC'></tfoot>

                              <tbody id='CW7DC'></tbody>

                            <small id='CW7DC'></small><noframes id='CW7DC'>