How to pass variables

Hello guys, I want to make a form on 2 different pages, like you complete some fields on first page and then depending on your answers, on second page you get some more form fields. To make this I need to pass variables between pages (and functions in controller).

something like this:

public function actionQuiz()

{


    $model = new Questions();





}

public function actionQuiz2($model)

{





}

Can you guys tell me how to get(pass) the data from 2 forms, like taking the data from first form, generate second form acording to data and then display final result for both forms.

I won’t say this is the perfect answer but you can use sessions or persist in the database if you want to save data between steps and recall it later on. Think about how shopping carts work.

Think about the requirements for the system… if the user closes the page, do you want to save the progress (database) or not (session)? Is this wizard for authenticated (logged in) users or anonymous users?

On validation… If you have separate forms for each ‘step’, you can generate a form model generated for each step, or else use a common model for all steps using scenarios to decide which validation rule applies for that step.

Thank you, but can you please show me an example how to pass a variable from a form to a session variable because I don’t seem to get it right.

I try to do something like this <?= $form->field($session[‘variable’], ‘username’)->textInput() ?> but it’s not working because it’s not an object.

The form field function is an object method, right? So in order to have the object populate your model code would need to grab session data either in the model or when it is created/loaded in the controller.

In the controller, when you instantiate the model:


$model = New QuizStepOne();

The next step would be to assign session data to the model’s attributes. Example:


$model->userId = $session['user']['id'];

Now your field() method on your form will work properly.

See this for more detail on accessing session variables

http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#access-session-data

or

http://www.bsourcecode.com/yiiframework2/session-handling-in-yii-framework-2-0/

Sorry for bothering you but I don’t fully get it, I should assing session data to the model’s attributes in the controller function right below after I instantiate it? But how then the form is going to work? Should I still use


field($session['variable'],..

or I need to use


field($model,..

now?

This is a bit messing with my head because it looks like you assign session data before gathering it.

I’m sorry but can you please give a bit more detailed example, thank you.

EDIT: Also I have a new question.

If I have many fields of same type, can I make a for loop and do something like:


$form->field($model, "bb[$i]")->textInput();

I would get this working with a single field using session before you introduce added complexity. Short answer is yes, but then you are implementing batch insert/update on your forms.

You need to use the field($model) syntax here. The original syntax won’t work for reasons we already discussed. Of course, you may choose to not use the

Lets start at the beginning and ask - what is your architecture plan. Do you want to:

  • Gather all information in temporary session storage, validate at the end, if valid, save

  • Gather each step, validate each step, save to session if valid, proceed to next step

  • Something else?

When you show the field, it will be empty unless you have saved session data. If you have saved session data, you will need to check if the session key iset/ !empty, and populate it. This really isn’t fundamentally different than how create vs. update works with gii-generated activerecord models.