yii 表单返回错误,参数未定义


yii Forms returns error, parameter is not defined

>我有像这样的设置控制器

public function actionIndex()
{
    $model = new SettingsForm;
    if(isset($_POST['SettingsForm'])) {
        if($model->validate()) {
            //
        }
    }
    $this->render('index', array('model' => $model));
}

并在"设置"视图中:

<?php
$form = $this->beginWidget(
    'CActiveForm', array(
       'id' => 'settings-form',
       'enableClientValidation' => true,
       'clientOptions' => array(
           'validateOnSubmit' => true,
       ),
));
?>
<div class="form-group">
<?php echo $form->labelEx($model, 'meta_keywords'); ?>
<?php echo $form->textField($model, 'meta_keywords', array('class' => 'form-control', 'value' => Yii::app()->config->get('meta_keywords'), 'placeholder' => 'Ключевые слова и фразы через запятую')); ?>
<?php echo $form->error($model, 'meta_keywords', array('class' => 'text-danger')); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model, 'main_page'); ?>
<?php echo $form->dropDownList($model, 'main_page', $model->getPages()); ?>
<?php echo $form->error($model, 'main_page', array('class' => 'text-danger')); ?>
</div>

函数 getPages 在设置窗体模型:

public function getPages() {
        return array(
            0 => 'Nothing'
        );
    }

此代码返回错误:

未定义属性"SettingsForm.main_page"。

但是所有以前的元素 Yii 都创建成功并且不返回任何错误 =''

在你的SettingsForm模型中main_pagerules方法中定义的吗?

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array( ... 'main_page', ...),
        ...
    );
}
/**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('site_name, charset, meta_description, meta_keywords', 'required'),
            array('main_page', 'boolean')
        );
    }

已更新

类设置窗体扩展 CFormModel

还是做吧。看看这里和这里(rus)。

public function rules()
{        
    return array(
        array('site_name, charset, meta_description, meta_keywords', 'required'),
        array('main_page', 'boolean'),
        array('site_name, charset, meta_description, meta_keywords, main_page', 'safe'),
    );
}

Pfffff....我只是忘记在模型中调用变量 $main_page...

class SettingsForm extends CFormModel
{
    public $site_name;
    public $charset;
    public $meta_description;
    public $meta_keywords;
    public $main_page;
..
}