Changes
Title
unchanged
How to use single form to collect data for two or more models (CActiveForm and Ajax Validation edition)
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
CActiveForm, ajax, several models
Content
changed
[...]
$b=new Info;
$this->performAjaxValidation(array($a,$b));
if(isset($_POST['User'],$_POST['Info']))
{
// populate input data to $a and $b
$a->attributes=$_POST['User'];
$b->attributes=$_POST['Info'];
if($a->save() && $b->save())
// validate BOTH $a and $b
$valid=$a->validate();
$valid=$b->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$a->save(false);
$b->save(false);
$this->redirect('index');
}
}
$this->render('create',array('a'=>$a,'b'=>$b));
}[...]
Thats all, now you have form with 2 models and ajaxValidation.
Links
-----
- [Chinese version](http://www.itkuaixun.com/bbs/thread-51-1-1.html "Chinese version")