I have two database, 1st one is for Group and the 2nd one is for Member. Now I want to validate the both models in a single form and the my group controller file is like this
public function actionCreate()
{
$model=new Group;
$member=new Member;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Group'],$_POST['Member']))
{
$model->attributes=$_POST['Group'];
//$member->attributes=$_POST['Member'];
if($model->save())
{
$member->attributes=$_POST['Member'];
$member->group_id = $model->id;
if($member->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'member'=>$member,
));
}
So how to make validate both models here.
You can create a class that extends
CFormModel. This keeps your controller code simple and clean. Make sure it can be created with the default constructor. Then you pass in the data for the group and user, validate it, and then save it, like this:The form class itself would create the group and member objects when you call
setData(), and it simply validates both classes invalidate(). Itssave()method would look like you’ve done above.