Consecutive Ajax form submissions

Hello,

I am dealing with the following situation:

I have a div which is updating with Ajax in 3 steps, rendering each time an active form replacing the previous one.

Clicking the corresponding submit button, an Ajax request is triggered . The Ajax controller saves the model (same for each step but using several attributes each time) and rendersAjax the next step.

Controller




$model= MyModel::findOne($id);

		

		if($model->load(Yii::$app->request->post()) && $model->validate())

		{

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

			$current_step=$_POST['current_step'];

			if($current_step==1)

			{

				$model->scenario="step2";

				$model->save();

				return $this->renderAjax('_step2');}

			else if($current_step==2)

			{

				$model->save();

				return $this->renderAjax('_step3');}

		}

		return $this->renderAjax('_step1'); 



The problem is that only at first step on click the form is validated as expected and renders step2.

But the view in step2 is already validated (incorrectly) without having clicked the submit button first (eg marking the required attributes) and allowing clicking for and rendering step3 regardless invalid input.

Is anyone familiar with the problem?

There is something wrong with your code. You are validating the form two times, in the


$model->validate()

and


$model->save()

You can achieve a step form using javascript. For the attribues validation use scenarios

Thank you very much for the answer and the help, I forgot to set false for runValidation in save().