私は2つのモデル、すなわちaclとuserを持っています。それぞれに独自の検証ルールのセットがあります。ただし、1つのフォーム内のすべての入力フィールドを使用します。
UserControllerには、createActionメソッド内に2つのajax検証メソッドがあります
これはこんな感じ
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();
}
}
検証しているフォームは1つだけで、誰でも提案できます