Changes
Title
changed
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
[...]
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->errorSummary(array($a,$b));
?>
<div class="row">[...]
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")