AJAX Form Validation


public function actionRegistration()

	{

		$model=new Userregistration();

		

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

		{

			echo CActiveForm::validate($model);

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

		}

		


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

		{

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

			print_r($_POST);


			if($model->validate())

			{

			$model->save();

			$this->actionIndex();

			}

		}		

		

		$this->render('registration',array('model'=>$model);

} 




<?php 

	$form=$this->beginWidget('CActiveForm', array(

		'id'=>'userregistration-registration-form',

		'enableAjaxValidation'=>true,

		'enableClientValidation'=>true, 

	));   ?>


public function rules()

	{

		return array(

			array('title, fname, lname, residence, contactType, contactNumber, gender, email, password, confrimpassword, admin', 'required'),

			array('email', 'length', 'max'=>512),

			array('email','email'),

			array('email','unique'),

                       ................................

			array('id, title, fname, lname, nationality, residence, contactType, contactNumber, gender, email, password, confrimpassword, admin', 'safe', 'on'=>'search'),

		);

	}

Ajax validation is also not working when I focus out of empty field.

When I enter an invalid email format the email field box turns red. It doesn’t show any message. And if I enter an existing email address it doesn’t give any error. Only when I click on form submit and the page reloads is when I get this error at the top of the form.

Please fix the following input errors:

Email &quot;zdsa@ffo.com&quot; has already been taken.

I have tried ‘clientOptions’=>array(‘validateOnSubmit’=>true), but the page still reloads

add this in your view


<?php echo $form->errorSummary($model); ?>

It’s already there

<?php echo CHtml::$afterRequiredLabel = ’ <span class=“pink-color”>*</span>’; echo $form->errorSummary($model); ?>

I have also tried ‘clientOptions’=>array(‘validateOnChange’=>true), but nothing happens when I move out of an empty field

try this


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'your-form-id',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>

doesn’t work. the page reloads

do you have javascript enabled check your browser this works for me i tested it

it’s enabled

does it show that the email cannot be blank when you leave the email field empty

Check if you’re getting any javascript errors in your browser’s error console. Some other script on the page could well be breaking all of the following scripts.

ok email field is working if I enter invalid email format and click on submit. It shows message in a box Please enter valid email. But if I leave the email field or any other field empty and click on submit the page reloads

Nothing appears on console while I’m typing or leaving out fields empty. But when I click on submit this appears on the console:

A form was submitted in the windows-1252 encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element.

Try this




$form=$this->beginWidget('CActiveForm', array(

                'id'=>'userregistration-registration-form',

                'enableAjaxValidation'=>true,

                'enableClientValidation'=>false, 

        ));   ?>



If im not wrong, you can’t have ‘enableAjaxValidation’ and ‘enableClientValidation’ defined ‘true’.

hey buddy by default it will only show you one error at a time

Client validation on Yii’s test site works without page reload when fields are left empty


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'login-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>

My code is




<?php 


	$country=CHtml::listData($countries,'country','country');


	$form=$this->beginWidget('CActiveForm', array(

		'id'=>'userregistration-registration-form',

		'enableAjaxValidation'=>false,

		'enableClientValidation'=>true,

		'clientOptions'=>array(

		'validateOnSubmit'=>true,

                ),		

	));   ?>

Why does my page reloads on submit when fields are empty ?

I have the exact same problem. Got not error feedback at all… ajax validation just doesn’t happen. It makes a normal submit :(

EDIT:

Turned out my “Chrome Cache Killer”-addon screwed things up :P

Problem solved!

Did you manage to fix this?

Yes, It’s working