Yii Framework Forum: performAjaxValidation in CActiveForm - Yii Framework Forum

Jump to content

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

performAjaxValidation in CActiveForm Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 March 2010 - 08:57 AM

I set up my form and controller as described here: http://www.yiiframew...lidation-detail

public function actionCreate()
{
	$model=new Product;
		
	$this->performAjaxValidation($model);

	if(isset($_POST['Product']))
	{
        	$model->attributes=$_POST['Product'];
        	if($model->save())
            		$this->redirect('index');
	}

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

}


protected function performAjaxValidation($model)
{
	if(isset($_POST['ajax']) && $_POST['ajax']==='product-form')
	{
		echo CActiveForm::validate($model);
		Yii::app()->end();
	}
}


<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'product-form',
	'enableAjaxValidation'=>true,
)); ?>


But Ajax validation does not get performed.
0

#2 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 16 March 2010 - 09:01 AM

Actually my mistake - the validation does get performed but only when the field loses focus - I was under the impression it would do it when the submit button is pressed - any way to make it work this way?
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 16 March 2010 - 02:13 PM

View PostGSTAR, on 16 March 2010 - 09:01 AM, said:

Actually my mistake - the validation does get performed but only when the field loses focus - I was under the impression it would do it when the submit button is pressed - any way to make it work this way?


<?php $form=$this->beginWidget('CActiveForm', array(
  'id'=>'product-form',
  'enableAjaxValidation'=>true,
  'validateOnSubmit'=>true,
  'validateOnChange'=>false,
  'validateOnType'=>false,
)); ?>

More info here:
http://www.yiiframew...tOptions-detail

(not tested)
/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
1

#4 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,035
  • Joined: 30-October 09
  • Location:UK

Posted 17 March 2010 - 04:22 AM

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'product-form',
	'enableAjaxValidation'=>true,
	'clientOptions'=>array('validateOnSubmit'=>true, 'validateOnChange'=>false),
)); ?>


This works OK, apart from that when the submit button is pressed it doesn't scroll up to the error summary, or to the first input error.
1

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