Yii Framework Forum: AJAX query - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

AJAX query Rate Topic: -----

#1 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 25 March 2010 - 07:14 PM

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.
0

#2 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 25 March 2010 - 08:16 PM

Hi frantic,

View Postfrantic, on 25 March 2010 - 07:14 PM, said:

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));
	}
}



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
0

#3 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 25 March 2010 - 08:33 PM

Hi, yoshi!

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

#4 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 26 March 2010 - 03:13 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users