php Yii dropdownlist 使用 $form-> dropdownlist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17020207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:07:07  来源:igfitidea点击:

Yii dropdownlist using $form-> dropdownlist

phpyii

提问by 5ome

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.

我想从 2 个不同的模型创建一个表单,第一个用于国家/地区,第二个用于文档。问题是我无法制作下拉列表,我总是收到错误消息。

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

这是代码,首先是我的 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));
    }

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.

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

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

好的,它没有定义,我尝试将其更改为“countriesIssued”,然后我收到另一个错误,提示为 foreach() 提供的参数无效

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

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

回答by Ionut Flavius Pogacian

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

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

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

回答by Sudhanshu Saxena

Make a list variable like this:

像这样创建一个列表变量:

In your Model:

在您的模型中:

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

And in your view:

在您看来:

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

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

回答by Jair

dropDownList($model,'state',array('1' => 'Do 1', '2' => 'Do 2'),array('selected' => 'Choose')); ?>

Or Yii 2

或 Yii 2

field($model,'state')->dropDownList(array('1' => 'Do 1','0' => 'Do 2'),array('options' => array('0' => array('selected' => true)))) ->label('State') ?>