Multimodel form validations

My database is just like this




====== Group ======

id

name


===== Member =====

id

group_id

firstname

lastname

membersince



For multimodel form I followed this link .

and now my Group controller actionCreate() method is like this




  public function actionCreate()

  {

    $model=new Group;

    $member=new Member;

    

    // Uncomment the following line if AJAX validation is needed

    // $this->performAjaxValidation($model);

    //$model->validate();

    

    if(isset($_POST['Group'],$_POST['Member']))

    {

      $model->attributes=$_POST['Group'];

      //$member->attributes=$_POST['Member'];

      if($model->save())

      {

        $member->attributes=$_POST['Member'];

        if($member->save())

        {

          $this->redirect(array('view','id'=>$model->id));

        }

      }

        //$this->redirect(array('view','id'=>$model->id));

    }


    $this->render('create',array(

      'model'=>$model,

      'member'=>$member,

    ));

  }

and my Group View _form.php is like this




<div class="form">


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

  'id'=>'group-form',

  'enableAjaxValidation'=>false,

)); ?>


  <p class="note">Fields with <span class="required">*</span> are required.</p>


  <!--<?php echo $form->errorSummary($model); ?>-->

<!--  <?php echo $form->errorSummary(array($model,$member));?>-->

  <?php echo $form->errorSummary(array($model,$member));?>

  


  <div class="row">

    <?php echo $form->labelEx($model,'name'); ?>

    <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>80)); ?>

    <?php echo $form->error($model,'name'); ?>

  </div>


  <div class="row">

    <?php echo $form->labelEx($member,'firstname'); ?>

    <?php echo $form->textField($member,'firstname',array('size'=>60,'maxlength'=>80)); ?>

    <?php echo $form->error($member,'firstname'); ?>

  </div>


  <div class="row">

    <?php echo $form->labelEx($member,'lastname'); ?>

    <?php echo $form->textField($member,'lastname',array('size'=>60,'maxlength'=>80)); ?>

    <?php echo $form->error($member,'lastname'); ?>

  </div>


  <div class="row">

    <?php echo $form->labelEx($member,'membersince'); ?>

    <?php

    $this->widget('zii.widgets.jui.CJuiDatePicker', array(

                  'name'=>'publishDate',

      // additional javascript options for the date picker plugin

      'options'=>array(

          'showAnim'=>'fold',

      ),

      'htmlOptions'=>array(

          'style'=>'height:20px;'

      ),

    ));

    ?>

  </div>


  <div class="row buttons">

    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

  </div>


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


</div><!-- form -->



Here both models are working good in save but when I am going to validate both models before save only the first model is doing valid but the second one is not doing any validate. Can some one guide me here.

If the second model (member) does not validate, what errors are returned?

Additionally, could you post the code that does the validation before saving?

Actually It is not showing any thing. Simply the blank fields are also going to save.

Your code is not optimal… as if there is a validation error in the $member the $model would be already saved…

Check again the article you referred to… you need to validate both models first… and only then save them both…