How Do Ajax-Based Validation Work Behind The Scenes?

I was re-reading the yii tutorial on Forms today and realized I’m utterly confused as to how the AJAX-based validation works. Consider this working code (default app generated by the yii command-line tool)




	public function actionLogin()

	{

		$model=new LoginForm;


		// if it is ajax validation request

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

		{

			echo CActiveForm::validate($model);

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

		}


                ...

	}



We create a new instance of LoginForm, and then send this model (without populating the models attributes) to the CActiveForm::validate() method.

How can this work without calling




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



or something simular?

When in doubt, check the API. In this case, $loadInput defaulting to true.




public static function validate($models, $attributes=null, $loadInput=true) 

{ 

    $result=array(); 

    if(!is_array($models)) 

        $models=array($models); 

    foreach($models as $model) 

    { 

        if($loadInput && isset($_POST[get_class($model)])) 

            $model->attributes=$_POST[get_class($model)];

        $model->validate($attributes); 

        foreach($model->getErrors() as $attribute=>$errors) 

            $result[CHtml::activeId($model,$attribute)]=$errors; 

    } 

    return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result);

}



You are absolutely right Sir, Thank you :)

Hello,

I have a question regarding the command line: is it posible to run ajax in command line?

I assume that yes, then the question extension is: how to do that?

Have a great day