AJAX query

Hi!

My versionf of yii is 1.1.1. I tried work with ajax but I atchived nothing. I want to realize the checking of user entered form parameters, just like in blog at yii package. I saw gread ajax work when tried adding the comment to the post. I did all the same in my project but nothing is working. Ajax request is sending, but not coming back.

I saw in firebug that class "validating" was added to div when I leave the input field, but after that neither "error" nor "success" class was added to one.

I have just two fields: usename and content.

Here is the form:


<div class="form">


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

	'id'=>'comment-form',

	'enableAjaxValidation'=>true,

)); ?>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>

	

	<div class="row submit">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>


<?php $this->endWidget(); ?>


</div>

and controller:


$model=new Comment;


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

{

	echo CActiveForm::validate($model);

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

}


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

{

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

		

	if ( $model->save() ) {

		Yii::app()->user->setFlash('commentSubmitted','Thank u');

		$this->redirect(array('show','id'=>$_GET['id'], 'pic'=>$_GET['pic'],'#'=>'cm'.$model->id));

	}

}

Rules from model:


public function rules()

{

	return array(

		array('content, username', 'required'),

	);

}

Data is sending exactly to this controller, I checked that too.

Hi frantic,

it’s not really obvious to me if you have an appropriate method called performAjaxValidation inside your controller?!

You would need something like:


protected function performAjaxValidation($model)

{

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

    {

       echo CActiveForm::validate($model); 

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

    }

}


public function actionCreate()

{

  $model=new Comment;

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

  {

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


      if ( $model->save() ) {

              Yii::app()->user->setFlash('commentSubmitted','Thank u');

              $this->redirect(array('show','id'=>$_GET['id'], 'pic'=>$_GET['pic'],'#'=>'cm'.$model->id));

      }

  }

}

Best regards,

yoshi

Hi, yoshi!

Thanks for your reply. But this method gives the same result. Only the "validating" class applies to the div.

Could anyone show me the working example of sending the form with ajax?