Changes
Title
unchanged
How to use a single form to collect data for two or more models?
Category
unchanged
Tutorials
Yii version
unchanged
Tags
changed
model validation
Content
changed
[...]
'b'=>$b,
));
}
```
And for the `create` view, we would needTo add these new fields to your Create form, add your 2nd table fields no stored in a 2nd model.
A's create.php:
```php
<?php echo $this->renderPartial('_form', array('a'=>$a, 'b'=>$b)); ?>
```
You usually place the specific fileds in the
_fo
llowing code,rm.php file if you are working off Gii created CRUD files.
```php
<?php echo CHtml::
fbeginForm(); ?>
<?php echo CHtml::errorSummary(array($a,$b)); ?>
<!-- ...input fields for $a, $b...
</form>
```
The above approach can also be used if we have more than two models to deal with.
Now, since version 1.0.3 and the `_form.php` view things change a litle. Folowing the example, we need to redlclare en the `create` view the parameters passed to the `create` view so the `_form` view can use them.
```php
<h2>Create view</h2 -->
<div class="row">
<?php echo $form->labelEx($a,'a_field'); ?>
<?php echo $form->textField($a,'a_field'); ?>
<?php echo $form->error($a,'a_field'); ?>
</div>
<div class="
actionBarrow">
[ <?php echo
CHtml::link('List',array('list')$form->labelEx($b,'b_field'); ?>
<?php echo $form->textField($b,'b_field'); ?>
]
[ <?php echo
CHtml::link('Manage',array('admin')$form->error($b,'b_field'); ?>
]
</div>
<?php echo
$this->renderPartial('_form', array(
'a'=>$a,'b'=>$b,
'update'=>false,
)); ?>
```
And in the `_form` work as ever (i.e. we can use $a and $b)
CHtml::endForm(); ?>
```
The above approach can also be used if we have more than two models to deal with.
### Links
[Ajax Validation Version](http://www.yiiframework.com/wiki/218/how-to-use-single-form-to-collect-data-for-two-or-more-models-cactiveform-and-ajax-validation-edition/)