Ajax Validation on Contact form isn't working

Hi everybody,

Something is bothering me on fresh install of yii.

Is that right to say that if you want ajaxValidation on the contact form you should proceed trough these steps ?

  1. In the siteController add the following line

$this->performAjaxValidation($model);

just after that one


$model=new ContactForm;

  1. In the view (contact.php) call the activform widget like this :

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

'id'=>'contact-form',

'enableAjaxValidation'=>true,

)); ?>

  1. Add this function into the siteController

protected function performAjaxValidation($model)

	{

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

		{

			echo CActiveForm::validate($model);

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

		}

	}

I must be missing something because these modifications don’t change anything to the whole behavior of the form.

I even cleared the assets directory. I’m quite sure i have to change something more in the view but I don’t know what !

I someone knows…

Ajax validation does the validation each time a field is change.

Add ‘validateOnSubmit’ if you need.

If you want to see the errors you need to put


<?php echo $form->error($model,'fieldname'); ?>

on every field in the contact form (it does not have these by default)…

NOTE: using ajax there could be some problems with the captcha - number of times it’s allowed to submit…

Hi Zaccaria, thanks for the answer.

my call to activeForm looks like this now


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

	'id'=>'contact-form',

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

	  'enableAjaxValidation'=>true,

)); ?>

But still no difference. I really think that beside this something has to be done on the view generated.

Ok Mdomba (waow this forum is impressive. Thank you guys)

Now my form looks like this


<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name'); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'email'); ?>

		<?php echo $form->textField($model,'email'); ?>

		<?php echo $form->error($model,'email'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'subject'); ?>

		<?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>

		<?php echo $form->error($model,'subject'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'body'); ?>

		<?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo $form->error($model,'body'); ?>

	</div>

Indeed, something changed. When i submit an empty form there is no "visible" validation at all. Nothing is diplayed no where but at least the page is not refreshed anymore and firebug tells ajax is working.

Why the error messages don’t display hence I can see this response via firebug


contact-form{"ContactForm_email":["Email is not a valid email address."],"ContactForm_verifyCode":["The verification code is incorrect."]}

Can it be a problem of response place holder ?

Before answering you… I tried to do the same… and it works for me…

Here is what I have done…

controller




	public function actionContact()

	{

		$model=new ContactForm;

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

		{

			echo CActiveForm::validate($model);

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

		}

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

		{

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

			if($model->validate())

			{

				$headers="From: {$model->email}\r\nReply-To: {$model->email}";

				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->refresh();

			}

		}

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

	}



view




...

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

	'id'=>'contact-form',

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


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


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name'); ?>

		 <?php echo $form->error($model,'name'); ?>

	</div>

...




Oh God ! I’ve found the trouble !!! Apparently my installation wasn’t so fresh as it seems to. I did another one just as you did. With the same tests and everything was ok !!!

I figure out that some jquery+css left overs copied from some other skeleton project was bugging the wole thing.

I’ve learn a great deal here. Next time I’ll exhaust all the possibilities of the framework before adding anything external to it !!!

You helped me a lot guys, sorry if I had you spent your time on this.

Thanks and have a nice day.

No problem… glad you solved it… and learned something new on the way :D