使用$ form->的Yii下拉列表下拉列表

时间:2013-06-10 08:58:19

标签: php yii

我想从2个不同的模型创建一个表单, 第一个是国家,第二个是文件。 问题是我不能制作下拉列表,我总是得到错误。

这是代码,首先是我的controller.php部分

$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));
    }

在我的视图脚本中,我使用此代码

      <?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(); ?>

但我收到此错误: 未定义属性“Country.countriesIssued”。

好吧它没有定义,我尝试将其更改为'countriesIssued',然后我又出现了另一个错误为foreach()提供的无效参数

如果有人可以帮助我。 我知道网上有更多的解决方案,我尝试但不理解,谢谢。

3 个答案:

答案 0 :(得分:6)

根据定义,listData的第一个参数是一个数组;你是一个对象;

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

答案 1 :(得分:2)

制作一个像这样的列表变量:

在你的模特中:

$countriesIssued = Country::model()->findAll(array('select'=>'code, name', 'order'=>'name'));

在你看来:

$list = CHtml::listData($countriesIssued, 'code', 'name'));

 echo CHtml::dropDownList('Your variable', Your $model, 
          $list,
          array('empty' => '(Select a category'));

答案 2 :(得分:1)

dropDownList($ model,&#39; state&#39;,array(&#39; 1&#39; =&gt;&#39; Do 1&#39;,&#39; 2&#39; =&gt;&# 39;做2&#39;),数组(&#39;选择&#39; =&gt;&#39;选择&#39;)); ?&GT;

或Yii 2

字段($模型,&#39;状态&#39;) - &GT; DROPDOWNLIST(       数组(&#39; 1&#39; =&gt;&#39;做1&#39;,&#39; 0&#39; =&gt;&#39;做2&#39;),数组(&#39;) ; options&#39; =&gt;数组(&#39; 0&#39; =&gt;数组(&#39;选择&#39; =&gt; true))))        - &GT;标签(&#39;状态&#39;)     ?&GT;
相关问题