Php yii表单返回错误,未定义参数
我有像这样的控制器Php yii表单返回错误,未定义参数,php,yii,yii-chtml,Php,Yii,Yii Chtml,我有像这样的控制器 public function actionIndex() { $model = new SettingsForm; if(isset($_POST['SettingsForm'])) { if($model->validate()) { // } } $this->render('index', array('model' => $model)); } 在“设置”
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.main_页”
但是,所有previos元素都已成功创建,并且在
设置中未返回任何错误=\。窗体模型是否在规则方法中定义了主页
?i、 e
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array( ... 'main_page', ...),
...
);
}
更新
类设置窗体扩展CFormModel
还是这样。看和看
Pfffff。。。。
我只是忘记了$model主页中的call变量
class SettingsForm extends CFormModel
{
public $site_name;
public $charset;
public $meta_description;
public $meta_keywords;
public $main_page;
..
}
隐马尔可夫模型。。。并且var\u dump($model->main\u page)
返回任何内容,或者也返回错误?你也可以在规则数组中尝试array('main_page','safe')
,看看booleanValidator是否有问题?@heeej,仍然请发布有问题的(作为更新)整个模型验证规则。我做了echo$form->dropDownList($model,'main_page',array(0=>'nothing');还有。。。。NOTHING+@Heeej,在下拉列表中,你只需放置$form->dropDownList($model,'main_page',array(0=>'smth'))
它会工作吗?@Heeej,还有,你需要打开ClientValidation吗?尝试将其关闭:'enableClientValidation'=>false,
因为您在服务器和下拉列表中进行验证,所以没有任何内容是从外部输入的。@heeeej,您仍然会在问题中发布(作为更新)整个模型验证规则,如下所示:公共函数规则(){return array(array('userId,email','required')),…
I post..I调用了所有字段=(当然,您没有将array('site\u name,charset,meta\u description,meta\u keywords,main\u page','safe','on'=>'search'),
定义为该数组中的最后一行。因此,在AR和DB中存储这些字段是不安全的。这样做吧。
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'),
);
}
/**
* @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')
);
}
class SettingsForm extends CFormModel
{
public $site_name;
public $charset;
public $meta_description;
public $meta_keywords;
public $main_page;
..
}