0

I have two models

1)TblRegistration : $model as object

 -director
 -experience
 -language

2)TblLogin : $model2 as object

 -email
 -password

Both fields are included in TblRegistration/_form.php

By defaut TblRegistration fields validation is included in rules().

views/tblRegistration/_form.php

<div class="row">
<?php echo $form->labelEx($model,'director'); ?>
<?php echo $form->textField($model,'director',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'director'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
 </div>

<div class="row">
<?php echo $form->labelEx($model,'language'); ?>
<?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'language'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model1,'email'); ?>
<?php echo $form->textField($model1,'email'); ?>
<?php echo $form->error($model1,'email'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model1,'password'); ?>
<?php echo $form->textField($model1,'password'); ?>
<?php echo $form->error($model1,'password'); ?>
</div>

models/TblRegistration.php

    public function rules()     {
     // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('director, experience, language', 'required'),
        array('experience', 'numerical', 'integerOnly'=>true),
        array('director, language', 'length', 'max'=>50),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('reg_id, director, experience, language', 'safe', 'on'=>'search'),
    );
    }

I want to include TblLogin fields into model/TblRegistraion rules for validation.

1 Answers1

0

You can manually validate a model by calling the validate() method:

if($modelA->validate() && $modelB->validate()) {
    // Call save method, fix foreign keys, etc
    $this->redirect(array('view'));
}

If there is an error the page will not be redirected so your form will reload. $form->error() will highlight the error fields. Also, when the first argument of errorSummary is an array containing your models, it will summarize them all for you.

Michiel
  • 2,143
  • 1
  • 21
  • 21