CListView passing $form? (CActiveForm)

I have a a form that uses about 6 data models, I tried using a CListView to break apart the HAS_MANY relations, but I can’t seem to pass the CActiveForm variable, read the documentation and searched the forum for a solution to no avail.

Extra values can be passed, but I can’t figure how to get $form to work.

Would you please explain your form structure with details?

What does it look like?




<form id="model-a">

   <div id="list-view-a">

      <div id="list-item-a-1">

        <input id="list-item-a-1-xxx" />

        <input id="list-item-a-1-yyy" />

      </div>

      <div id="list-item-a-2">

        <input id="list-item-a-2-xxx" />

        <input id="list-item-a-2-yyy" />

      </div>

      ...

   </div>

</form>



The above is just an example, but you could show us something like this.

Or, maybe the simplified versions of your main form view file and the partial view file for the CListView would be better.

This is my current working _form.php:


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

'id'=>'products-form',

    'htmlOptions'=>array(

    'enctype'=>'multipart/form-data'),

'enableAjaxValidation'=>false,

)); ?>

...........

<table>

<?php  foreach($prodcats as $i=>$prodcat) { ?>

  <tr>  

<?php echo $form->hiddenField($prodcat, "[$i]prod_cat_id") ?>       

<td><?php echo $form->dropDownList($prodcat,"[$i]cat_id",$catList, $htmlOptions=array('empty'=>'Select Category')); ?><?php echo $form->error($prodcat,"[$i]cat_id"); ?></td>

<td><?php echo CHtml::link('Delete', $url=array('products/deleteProdCat', 'prod_cat_id'=>$prodcat->prod_cat_id));?></td>

    </tr>

<?php 

}

if(!empty($prodcats)){ $i++; }

$prodcat = new ProdCat();

?>

    <tr>

<td><?php echo $form->dropDownList($prodcat,"[$i]cat_id",$catList, $htmlOptions=array('empty'=>'Select Category')); ?><?php echo $form->error($prodcat,"[$i]cat_id"); ?></td><td>New</td>

    </tr>

</table>

......

I was trying to replace that example with (Please note it is simplified):


<?php

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

	'dataProvider'=>$prodcats,

    'itemView'=>'_cats',

    'template'=>'{items}'

)); 

?>

This would work in the _cats view file:




echo $data->prod_cat_id



This won’t work


<?php

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

	'dataProvider'=>$prodcats,

    'itemView'=>'_cats',

    'template'=>'{items}',

'viewData'=>$form

)); 

?>


echo $form->hiddenField($data, 'prod_cat_id') (or something like that probably needs to be $data->form)

Because $form is an object and viewData needs to be an array, passing an array would break break the CAvtiveForm, I think.

I see.

I wonder if CListView accepts an array as ‘dataProvider’ or not …???

I’m not sure, but it should be:




    'dataProvider' => new CArrayDataProvider($prodcats),



And the syntax for ‘viewData’ is:




    'viewData' => array('form'=>$form),



So, this might work:




<?php

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

    'dataProvider' => new CArrayDataProvider($prodcats),

    'itemView'=>'_cats',

    'template'=>'{items}',

    'viewData'=> array('form'=>$form),

)); 

?>



But I would rather use renderPartial:




<?php

foreach($prodcats as $i=>$prodcat) {

    $this->renderPartial('_cats', array('form'=>$form, 'i'=>$i, 'prodcat'=>$prodcat));

}

?>



It looks more simple to me. :)

Wow, I feel real stupid changing


'viewData'=>$form

to this


'viewData'=>array('form'=>$form)

fixed it.

I was using CActiveDataProvider to return $prodcats, but it’s good to know I can use CArrayDataProvider on queries that return arrays.

I used CListView so on other models I can use the sort feature.

Now this is my next headache:

http://www.yiiframework.com/forum/index.php/topic/32306-retrieve-attributes-of-joined-model/