微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用$form-> dropdownlist的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()提供了无效参数.

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

解决方法:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐