nested form

Hi,

I’m trying to get nested forms working like it’s described in the definitive guid to yii…

So I’m having my actionCreate function in my controller like that




public function actionCreate()

{

    $form = new CForm('application.views.contact.contactForm');

    $form['contact']->model = new Contact;

      

    if($form->submitted('createCon') && $form->validate())

    {

      $contact = $form['contact']->model;

      if($contact->save(false))

      {

        $this->redirect(array('site/index'));

      }

    }

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

}



my contactForm.php:


<?php


/** 

 * Registration Form

 */ 

 

 return array(

  'elements'=>array(

    'contact'=>array(

      'type'=>'form',

      'title'=>'Contact information',

      'elements'=>array(

        'uid'=>array(

          'type'=>'hidden',

          'value'=>Yii::app()->user->id,

        ),

        'streetNum'=>array(

          'type'=>'text',

          'label'=>'Street/Number',

        ),

        'zip'=>array(

          'type'=>'text',

          'label'=>'Zip code',

        ),

        'city'=>array(

          'type'=>'text',

          'label'=>'City',

        ),

        'country'=>array(

          'type'=>'dropdownlist',

          'label'=>'Country',

          'items'=>array('Germany'=>'Germany','UK'=>'UK'),

        ),

        'phone'=>array(

          'type'=>'text',

          'label'=>'Telephone',

        ),

        'fax'=>array(

          'type'=>'text',

          'label'=>'Fax',

        ),

        'website'=>array(

          'type'=>'text',

          'label'=>'Website',

        ),

      ), 

    ),

    

  ),

  'buttons'=>array(

    'createCon'=>array(

      'type'=>'submit',

      'label'=>'Register',

    ),

  ),

  

 );

?>  

and my create-view:


<?php

$this->breadcrumbs=array(

	'Contacts'=>array('index'),

	'Create',

);


$this->menu=array(

	array('label'=>'List Contact', 'url'=>array('index')),

	array('label'=>'Manage Contact', 'url'=>array('admin')),

);

?>


<h1>Create Contact</h1>


<div class="form">

<?php echo $form; ?>

</div>

if I do it like that it works fine.

I’m now trying to have more information which is stored in a different data model to be displayed on the same page, changing the actionCreate and the contactForm.php to


public function actionCreate()

{

    $form = new CForm('application.views.contact.contactForm');

    $form['contact']->model = new Contact;

    $form['candidate']->model = new Candidate;

      

    if($form->submitted('createCon') && $form->validate())

    {

      $contact = $form['contact']->model;

      $candidate = $form['candidate']->model;

      if($contact->save(false))

      {

        $candidate->contactId = $contact->id;

        $candidate->save(false);

        $this->redirect(array('site/index'));

      }

    }

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

}


<?php


/** 

 * Registration Form

 */ 

 

 return array(

  'elements'=>array(

    'contact'=>array(

      'type'=>'form',

      'title'=>'Contact information',

      'elements'=>array(

        'uid'=>array(

          'type'=>'hidden',

          'value'=>Yii::app()->user->id,

        ),

        'streetNum'=>array(

          'type'=>'text',

          'label'=>'Street/Number',

        ),

        'zip'=>array(

          'type'=>'text',

          'label'=>'Zip code',

        ),

        'city'=>array(

          'type'=>'text',

          'label'=>'City',

        ),

        'country'=>array(

          'type'=>'dropdownlist',

          'label'=>'Country',

          'items'=>array('Germany','UK'),

        ),

        'phone'=>array(

          'type'=>'text',

          'label'=>'Telephone',

        ),

        'fax'=>array(

          'type'=>'text',

          'label'=>'Fax',

        ),

        'website'=>array(

          'type'=>'text',

          'label'=>'Website',

        ),

      ), 

    ),

    'candidate'=>array(

      'type'=>'form',

      'title'=>'Personal information',

      'elements'=>array(

        'uid'=>array(

          'type'=>'hidden',

          'value'=>Yii::app()->user->id,

        ),

        'firstName'=>array(

          'type'=>'text',

          'label'=>'First Name',

        ),

        'lastName'=>array(

          'type'=>'text',

          'label'=>'Last Name',

        ),

        'birthday'=>array(

          'type'=>'text',

          'label'=>'Birthday',

        ),

        'sex'=>array(

          'type'=>'radiolist',

          'label'=>'Sex',

          'items'=>array('m'=>'male','w'=>'female'),

        ),

        'instrument'=>array(

          'type'=>'dropdownlist',

          'label'=>'Instrument',

          'items'=>array('Cello'=>'Cello','Violin'=>'Violin'),

        ),

        'nationality'=>array(

          'type'=>'text',

          'label'=>'Nationality',

        ),

      ),

    ),

    

  ),

  'buttons'=>array(

    'createCon'=>array(

      'type'=>'submit',

      'label'=>'Save',

    ),

  ),

  

 );

?>  

now it doesn’t store anything to my databases, I think it’s because it needs the contactId, which should have been passed with $candidate->contactId = $contact->id; but somehow didn’t. How can I do that? The hidden field works well for the user ID but since I don’t have the contactId yet because I’m just creating that it doesn’t work. Or maybe I just skip this alltogether and reference everything to the user ID…

The second question is: how do I get an array to my dropdownlist? I have a XML file which i put to an array with this method:


public function returnArr($file, $lang, $tag)

  {

    $xmlF = Yii::app()->basePath.'\data\\'.$file;

    if (file_exists($xmlF)) {

      $xml = simplexml_load_file($xmlF);

    }

    foreach ($xml->$lang->$tag as $value) {

      $cArr[$value] = $value;

    }

 

    return $cArr;

  }

But I putting that method into my contactForm.php doesn’t work because this seems to be just the view…

Thanks,

schlydi

bump