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

时间:2014-11-26 13:06:01

标签: php yii yii-chtml

我有像

这样的设置控制器
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>

在SettingsForm模型中运行getPages:

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

此代码返回错误:

  

Property&#34; SettingsForm.main_page&#34;没有定义。

但是Yii创建的所有优先元素都没有返回任何错误= \

4 个答案:

答案 0 :(得分:0)

SettingsForm方法中定义的main_page模型是rules吗?即。

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array( ... 'main_page', ...),
        ...
    );
}

答案 1 :(得分:0)

更新

  

class SettingsForm扩展了CFormModel

仍然这样做。查看herehere (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'),
    );
}

答案 2 :(得分:0)

/**
     * @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')
        );
    }

答案 3 :(得分:0)

Pfffff .... 我忘了模型$ main_page中的调用变量...

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

..
}