dergus/yii2-whitewizard

一个用于轻松处理多步表单的 Yii2 扩展。

安装: 123

依赖: 0

建议者: 0

安全: 0

星星: 1

观察者: 3

分支: 0

公开问题: 0

类型:yii2-extension

v1.0.1 2015-11-28 19:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:58:59 UTC


README

一个用于轻松处理多步表单的 Yii2 扩展。

安装

安装此扩展的首选方法是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist dergus/yii2-whitewizard "*"

或者在您的 composer.json 文件的 require 部分添加以下内容:

"dergus/yii2-whitewizard": "*"

用法

使用此操作的两种方式:1) 每一步都是正常的 yii 操作。2) 动作中的所有步骤都通过 http 变量区分

==============================================

如何以第一种方式使用扩展


<?php
namespace app\controllers;

use Yii;
use app\models\First;
use app\models\Second;
use app\models\Third;
use dergus\whitewizard\WhiteWizard;

class SiteController extends \yii\web\Controller
{

//first you need to add extension in 
//controllers behaviors with configuration:
    public function behaviors()
    {
	    /**
	     * controller actions which
	     * are available only in
	     * step chain
	     * @var array
	     */
        $closedActions=['confirmation'];
            /**
     * array of steps in order
     * in which the wizard
     * will process them
     * can be in two formats:
     * 1-st, when steps are
     * action ids:
     * ['first','second','third'].
     * 2-nd, when all the steps in on action
     * and they're distinguished by query
     * param, it shuold associative
     * array where the keys are action ids
     * and the values are arrays with
     * step names:
     * ['index'=>[
     *     'first',
     *     'second'
     * ],'contact'=>[
     *    'first',
     *    'second' 
     * ]]
     * 
     * 
     * @var array
     */
        $steps[]='first';
        $steps[]='second'

        return [

            'WhiteWizard'=>[
                'class'=>WhiteWizard::className(),
                '_steps'=>$steps,
                '_closedActions'=>$closedActions,
                /**
			     * step after which
			     * user can't go backward
			     * @var string
			     */
                '_noBackStep'=>'second'

            ],

        ];
    }

    Then in actions:

    public function actionFirst()
    {

		
        $model=new First;

        //must do to show user his data
        //if he went back

        if(($s=Yii::$app->session->get('first'))!==null){
            $model=$s;
        }
        if ($model->load(Yii::$app->request->post())) {
            if($model->validate()){
                $this->handleStep();//must call if the step is successfull
                Yii::$app->session->set('first',$model);//must do to save data from this step for further saving
                $next='second';
                return $this->redirect([$next]);}
            else{
            	//must call if data is not valid
                $this->notHandled();
            }
        } 

		return $this->render('first',compact('model'));
    }

All the same:

    public function actionSecond()
    {

		
        $model=new Second;

        //must do to show user his data
        //if he went back

        if(($s=Yii::$app->session->get('second'))!==null){
            $model=$s;
        }
        if ($model->load(Yii::$app->request->post())) {
            if($model->validate()){
                $this->handleStep();//must call if the step is successfull
                Yii::$app->session->set('second',$model);//must do to save data from this step for further saving
                $next='third';
                return $this->redirect([$next]);}
            else{
            	//must call if data is not valid
                $this->notHandled();
            }
        } 

		return $this->render('third',compact('model'));
    }

And the last step shuld be a little bit different:

    public function actionThird()
    {

		
        $model=new Third;

        //must do to show user his data
        //if he went back

        if(($s=Yii::$app->session->get('third'))!==null){
            $model=$s;
        }
        if ($model->load(Yii::$app->request->post())) {

        	//the implemenatation of $model->saveData() method
        	//is up ti you but it should save data from all the steps.
        	//Data from previous steps can be accessed from session
			//by key you save it, for example:
			//Yii::$app->session->get('first')

            if($model->validate() && $model->saveData()){
                
                //claen the session

                Yii::$app->session->set('first');
                Yii::$app->session->set('second');
				Yii::$app->session->set('third');
				$this->allDone();
                $next='success';
                return $this->redirect([$next]);}

        } 

		return $this->render('success',compact('model'));
    }

}

第二种方式的用法(不推荐,因为会使代码混乱)

---- 将稍后添加 ----