Regarding Ajax Validation

I am trying to understand how the framework processes form data when using AJAX validation. For example with the following standard code:




$model = $this->loadModel($id);

		

if(isset($_POST['ajax']) && $_POST['ajax']==='listing-form')

{

	echo CActiveForm::validate($model);

	Yii::app()->end();

}

		

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

{

	$model->attributes = $_POST['Listing'];

			

	if($model->save())

	$this->redirect(array('view', 'id'=>$model->id));

}



If validation is successful, how does it continue with the code - for example how does it continue past Yii:app()->end() ?

It does not. It is Ajax. The javascript receives the information that is being produced by the echo statement, which will be JSON. When there are no errors that will be an empty result (I think) and the javascript does not show any errors. When there are errors in the JSON response the Javascript takes care of showing it on the screen.

After the output of the JSON the script should end. It does not go past Yii::app()->end();

Yes that is all correct, however what I don’t understand is that after echo’ing the errors there is “Yii::app()->end();”.

So when there are errors, the "Yii::app()->end();" ensures that it does not go any further in the code. However when there are no errors, how does it go further to process the normal form POST data?

Assuming that you have set:


'validateOnSubmit'=>true,

'validateOnChange'=>false,

On submit first the Ajax request is posted and when that returns without errors a second (not Ajax) request is posted.