Collecting Cactiveform Errors On Server-Side

Hello,

I have a CActiveForm with this summary:


	.

	.

	.

	'id'='email-form',

	'enableAjaxValidation`=>true,

	'clientOptions' => array('validateOnSubmit'=>true),

	.

	.

	.

Now i intend collect form Errors in Server-Side and sent it via json object to the client. in Client-Side there

is and Jquery function that parse the json object(form Errors) and set data to errorSummary and at last

shows the errorSummary of form.

i have done it without any problem, my question is what following functions don’t collect form Errors:


	protected function getErrorSummary($model)

	{

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

        	$errors=CActiveForm::validate($model);

        	if($errors !== '[]')

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

    	}

	}

But following collect form Errors:


	protected function getErrorSummary($model)

	{

        	$errors=CActiveForm::validate($model);

        	if($errors !== '[]')

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

	}

notice that both functions truly acts on validateOnChange.

Hi there

Why would you wanna invent your on ajax logic since yii does give you a method out of the box use that

here is Yii version of your code




protected function getErrorSummary($model)

{

    if(Yii::app()->request->isAjaxRequest) {

        $errors=CActiveForm::validate($model);

        if($errors !== '[]')

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

    }

}



Excuse me, but my problem isn’t understanding what’s the request type! however your code didn’t work.

excuse you, then should describe you problem properly, I have replied according to what you have posted above

I want know why the following method don’t collect model errors:


        protected function getErrorSummary($model)

        {

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

                $errors=CActiveForm::validate($model);

                if($errors !== '[]')

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

        }

        }

unless in form(CActiveForm) submission action dose not send ajaxVar(ajax) with form ID value, so why is not accessible here?

do a dump of your $_POST and see, if you are getting that $_POST[‘ajax’] with value that you checking in your condition

In document is:(clientOptions)

but i don’t find ajaxVar in var_dump($_SERVER)!

then there is something wrong with your form

Following var_dump($_POST) is for when a field(newEmail) is left empty:




array

  'User' =>

	array

  	'email' => string 'user@gmail.com' (length=14)

  	'newEmail' => string '' (length=0)



Following var_dump($_POST) is for when all the fields are filled:




array

  'User' =>

	array

  	'email' => string 'user@gmail.com' (length=14)

  	'newEmail' => string 'admin@minudasht.net' (length=19)

  'ajax' => string 'email-form' (length=10)

  'yt0' => string 'update' (length=18)



This means ajaxVar is initialized only when form is in submission action?

try the double equals instead of triple


protected function getErrorSummary($model)

{

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

        $errors=CActiveForm::validate($model);

        if($errors !== '[]')

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

    }

}

Here i understand what when ajaxVar initialized. When [color="#0000FF"]‘validateOnSubmit’=>true[/color] is set ajaxVar initialized onsubmit, and when[color="#0000FF"] validateOnChange=>true[/color] is set ajaxVar initialized on changing fileds.

and know understand what’s main problem: newEamil field as default is empty and when submitting the form without change on this field ajaxVar don’t initialized even if validateOnChange=>true is set. This problem can be solved?

The role is:


array('email,newEmail','required', 'on'=>'CEmail', 'message'=>$required_msg),

Useful Link