gpt4 book ai didi

php - Yii 下拉列表使用 $form-> dropdownlist

转载 作者:可可西里 更新时间:2023-11-01 00:09:37 25 4
gpt4 key购买 nike

我想从 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));
}

在我的 View 脚本中我使用这段代码

      <?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”,然后我收到另一个错误,提示 Invalid argument supplied for foreach()

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

最佳答案

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

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

关于php - Yii 下拉列表使用 $form-> dropdownlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com