How to use single for to collect data for two or more models (CActiveForm and Ajax Validation edition)

You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#4) »

With Yii you can use one CActiveForm for collecting data for two or more models with ajax validation and client validation.

According this article you can also create a form with ajax validation for both models.

You have two models $a and $b.


<?php $form = $this->beginWidget('CActiveForm', array(
)); ?>

<?php echo $form->errorSummary(array($a,$b));


<div class="row">
    <?php echo $form->labelEx($a,'firstName'); ?>
    <?php echo $form->textField($a,'firstName'); ?>
    <?php echo $form->error($a,'firstName'); ?>
<div class="row">
    <?php echo $form->labelEx($b,'lastName'); ?>
    <?php echo $form->textField($b,'lastName'); ?>
    <?php echo $form->error($b,'lastName'); ?>

<?php $this->endWidget(); ?>


public function actionCreate()
    $a=new User;
    $b=new Info;

        if($a->save() && $b->save())

protected function performAjaxValidation($models)
    if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
        echo CActiveForm::validate($models);

Thats all, now you have form with 2 models and ajaxValidation.

15 0
Viewed: 58 830 times
Version: Unknown (update)
Category: How-tos
Written by: RusAlex
Last updated by: Darwin Wen
Created on: Jul 20, 2011
Last updated: 6 years ago
Update Article


View all history

Related Articles