Yii multi page form wizard best practice

前端 未结 2 674
借酒劲吻你
借酒劲吻你 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 18:00

    Yii provides a feature called page states to implement things like a multi step / multi page form wizard.

    Lets have a look at the Yii docs first:

    A page state is a variable that is persistent across POST requests of the same page. In order to use persistent page states, the form(s) must be stateful which are generated using {@link CHtml::statefulForm}.

    So the forms of every step / page need to be stateful forms. To render a stateful form you just need to set the CActiveForm::stateful property to true when you start the ActiveForm-widget. Within your controller you can get and set your page states with CController::getPageState() or CController::setPageState().

    So these are the basics that work quite well if the implementation of your multi page form wizard is made in the traditional style without AJAX requests.

    If however you want to use AJAX calls to submit step data and display the next step, Yii's page states are not usable.

    Why? All the page states are transported through HTTP-POST within a hidden input field. The input field gets filled by Yii while the so called output processing. The output processing starts after the rendering and will replace parts of the output. So Yii's page states feature requires the output processing. AJAX responses on the other hand may become corrupted by it because the output processing may also add or

提交回复
热议问题