5ome 5ome - 4 months ago 22
PHP Question

Yii dropdownlist using $form-> dropdownlist

i want to create a form from 2 different models,
1st is for countries, and the 2nd is for documents.
The problem is that i can't make a dropdown list, i get the errors all the time.

Here's the code, first my controller.php part

$model = new Country;
$model2 = new Product;

$this->performAjaxValidation(array($model, $model2));
if(isset($_POST['Country'],$_POST['Product']))
{
// populate input data to $model and $model2
$model->attributes=$_POST['Country'];
$model2->attributes=$_POST['Product'];

// validate BOTH $model and $model2
$valid=$model->validate();
$valid=$model2->validate() && $valid;

if($valid)
{
// use false parameter to disable validation
$model->save(false);
$model2->save(false);

$this->redirect('index');
}
}
...
$countriesIssued = Country::model()->findAll(array('select'=>'code, name', 'order'=>'name'));
...
$this->render('legalisation', array('model'=>$model, 'model2'=>$model2, 'documents'=>$documents, 'countriesIssued'=>$countriesIssued, 'countries'=>$countries, 'flag'=>$flag));
}


In my view script I use this code

<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>

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

<?php echo $form->dropDownList($model, 'countriesIssued',
CHtml::listData($countriesIssued, 'code', 'name'));?>


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


but i get this error :
Property "Country.countriesIssued" is not defined.

Ok it's not defined, i try to change it to 'countriesIssued', then i got another error saying Invalid argument supplied for foreach() .

If anybody can help me please.
I know there is more solutions on net, i try it but not understand, Thanks.

Answer

By definition, the first param of listData is an array; Your is a object;

    <?php 
echo $form->dropDownList($model, 'classification_levels_id', CHtml::listData(ClassificationLevels::model()->findAll(), 'id', 'name'),$classification_levels_options);
?>
Comments