I have two models namely acl and user. Each have there own set of validation rules. I however use all input fields within one form.
In the UserController I have two ajax validation methods within the createAction method
This looks like this
public function actionCreate()
{
$model=new user;
$acl = new acl;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation1($acl);
$this->performAjaxValidation($model);
//$this->performAjaxValidation1($acl);
//$valid=$model->validate();
//$valid=$acl->validate() && $valid;
if(isset($_POST['user'], $_POST['acl']))
{
$model->attributes=$_POST['user'];
$acl->attributes=$_POST['acl'];
if($model->save() && $acl->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'acl'=>$acl
));
}
AjaxValidation
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation1($acl)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($acl);
Yii::app()->end();
}
}
Only one form is validating
can anyone sugges
From the
CActiveForm‘svalidate()documnentation, you can pass an array of models. You only need to declare one ajax validation function. Below is the modified codeController Action
Ajax Validation function