Multi page form - validation with scenarios

What I have is a 3 view form:

View 1 is a form and has model scenario ‘Send’ (view.php)

View 2 is a form and has model scenario ‘Sent’ (send.php)

View 3 is the confirmation page (sent.php)

The form method is POST. At the moment the form actions are blank (submitting to the same URL). I need access to the previous forms’ POST variables on each stage.

How can I perform validation for each form, and then if successful display the next view/form?

I tried doing it using $this->redirect() but then I obviously lose my POST variables. I did also include a list of variables as the parameter of the redirect() method but because I have URLFormat "path" it was not generating the parameters correctly in the URL.

I am using $model->setScenario() to set the appropriate scenario in my controller actions. But the main problem I’m having is that scenarios need to be set before form submission and goes hand in hand with the validation.

What is the correct approach for this, and what actions do I need in my controller?

So ideally I want the page URL to change from view.php > send.php > sent.php but if validation fails at any stage then redirect back to the previous url with the errors shown.

BUMP.

Anybody have any thoughts on this? Maybe use session variables?

The most compact way I can think of at the moment is something along these lines:

You’ll need to hardcode “stages” in your views, or like you said, add them to your session.


public function actionAdvancedForm()

{

  $advancedForm = new AdvancedForm();


  $view = 'view1';

  if (isset($_POST['AdvancedForm']))

  {

    $advancedForm->attributes = $_POST['AdvancedForm'];


    $stagesToScenarios = array(

      array('scenario'=>'Send', 'view'=>'view1', 'failView'=>'view1'),

      array('scenario'=>'Sent', 'view'=>'view2', 'failView'=>'view1'),

      array('scenario'=>'Confirmation', 'view'=>'view3', 'failView'=>'view2'),

    );


    $stageInfo = $stagesToScenarios[$advancedForm->stage];


    $advancedForm->scenario = $stageInfo['scenario'];

    if ($advancedForm->validate())

    {

      $view = $stageInfo['view'];

    }

    else

    {

      $view = $stageInfo['failView'];

    }


  }


  $this->render($view, array('model'=>$advancedForm));

}

Yii had a clever way of handling multi-view forms, but I can’t seem to recall where.

NOTE: I have not tested this code, just busted it out real quick in notepad++.

Cheers NSF. In the end I’ve decided to use sessions, as it’s probably the easiest way to do it.