GSTAR
(Omzy83)
March 16, 2010, 1:57pm
1
I set up my form and controller as described here: http://www.yiiframework.com/doc/api/CActiveForm#enableAjaxValidation-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.
GSTAR
(Omzy83)
March 16, 2010, 2:01pm
2
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?
tri
(tri - Tommy Riboe)
March 16, 2010, 7:13pm
3
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'product-form',
'enableAjaxValidation'=>true,
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'validateOnType'=>false,
)); ?>
More info here:
http://www.yiiframework.com/doc/api/CActiveForm#clientOptions-detail
(not tested)
/Tommy
GSTAR
(Omzy83)
March 17, 2010, 9:22am
4
<?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.