Getting error in Yii Multimodel Form

2.5k views Asked by At

I am a newbie to the Yii framework.I want a multimodel form so I just went through this link and made all things like this.I have two table, first is group and another is member.

Group

 ID
 name

 Member

 id
 group_id
 firstname
 lastname

Now I have made models for both tables and CRUD as well.I made change to GroupController file like this

public function actionCreate()
  {
    $group = new Group;
    $member = new Member;
    if(isset($_POST['Group'],$_POST['Member'])) {
    //Populate input data to $group and $member
      $group->attributes = $_POST['Group'];
      $member->attributes = $_POST['Member'];

    //Validate both $group and $member
      $validate = $group->validate();
      $validate = $member->validate() && $valid;

      if($valid){
        $group->save(false);
        $member->save(false);
      }
    }
    $this->render('create',array(
      'group'=> '$group',
      'member'=> '$member',
    ));
    $model=new Group;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Group']))
    {
      $model->attributes=$_POST['Group'];
      if($model->save())
        $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
      'model'=>$model,
    ));
  }

and after changing the group >> View >> create.php file like this

<?php echo $this->renderPartial('_form', array('group'=>$group, 'member'=>$member)); ?>

The _form file 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($group,$member); ?>

  <div class="row">
    <?php echo $form->labelEx($model,'name'); ?>
    <?php echo $form->textField($model,'name'); ?>
    <?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'=>128)); ?>
    <?php echo $form->error($member,'firstname'); ?>
  </div>

  <div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
  </div>

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

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

but after all I am getting error like this Undefined variable: group . So can some one please tell me how to solve this issue. I have lost one day behind this.So any help and suggestions will be highly appreciable.

1

There are 1 answers

4
Mukesh Soni On

You are doing multiple mistakes here ->

when you call

 $this->render('create',array(
      'model'=>$model,
    ));

you are not passing $group or $member models which you created in the group create controller. Change it to -

 $this->render('create',array(
      'group'=>$group,
      'member'=>$member,
    ));

and secondly, there is no variable named $valid... change this part

$validate = $member->validate() && $valid;

  if($valid){

to

$validate = $member->validate() && $validate;

      if($validate){

now things should work fine