我是yii的新手。我有這個問題,我的數據存儲在radiobuttonlist或dropDownList沒有保存在數據庫中。它總是顯示爲空。這裏是我的代碼使用RadioButtonList或dropDownList時保存爲空數據
查看:
<?php
$form = $this->beginWidget('CActiveForm');
echo $form->label($model,'gender');
echo $form->radioButtonList($model,'gender',array('M'=>'Male','F'=>'Female'));
echo $form->label($model,'cat');
echo $form->dropDownList($model,'cat',$category);
echo CHtml::submitButton('Submit');
$this->endWidget();
?>
控制器:提前
public function actionCreate()
{
$model=new Test;
if(isset($_POST['Test']))
{
$model->attributes=$_POST['Test'];
if($model->save()){
$this->redirect(array('index'));
}
else
var_dump($model->errors);
}
$cat = array('st'=>'STAFF','ot'=>'OTHERS');
$model->gender='M';
$this->render('create',array(
'model'=>$model,'category'=>$cat
));
}
請幫助...謝謝
編輯:加入後需要的在規則部分呢就像一個魅力
那麼這裏是修改後的測試模型
public function rules()
{
return array(
array('gender,cat', 'required'),
array('name', 'length', 'max'=>45),
);
}
是的你是對的。我忘了爲性別和貓列添加所需的規則。我已更新問題 – Bahdeng 2012-03-16 03:25:50