我正在使用Yii中的ajax crud。我在表格中有一个radiobuttonlist。我得到了'不能空白'即使被选中也会出错。请帮我找到问题。
在ajax_form中,代码是:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'universal-electives-form',
// 'htmlOptions' => array('enctype' => 'multipart/form-data'),
'action' => $actionUrl,
// 'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'name'),
'errorMessageCssClass' => 'input-notification-error error-simple png_bg',
'clientOptions'=>array('validateOnSubmit'=>true,
'validateOnType'=>false,
'afterValidate'=>'js_afterValidate',
'errorCssClass' => 'err',
'successCssClass' => 'suc',
'afterValidate' => 'js:function(form,data,hasError){ $.js_afterValidate(form,data,hasError); }',
'errorCssClass' => 'err',
'successCssClass' => 'suc',
'afterValidateAttribute' => 'js:function(form, attribute, data, hasError){
$.js_afterValidateAttribute(form, attribute, data, hasError);
}'
),
));
<?php echo $form->labelEx($model,'offered_for',array('style'=>'color:#000000')); ?>
<?php echo $form->radioButtonList($model,'offered_for',array('1'=>'Boys','2'=>'Girls','0'=>'Both')); ?>
<?php echo $form->error($model,'offered_for'); ?>
在模型中,规则是:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, desc, batch, offered_for, created_by, created_at', 'required'),
array('offered_for, created_by', 'numerical', 'integerOnly'=>true),
array('name, desc, batch', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, desc, batch, offered_for, created_by, created_at', 'safe', 'on'=>'search'),
);
}
在控制器中:
public function actionAjax_Create(){
if(isset($_POST['UniversalElectives']))
{
$model=new UniversalElectives;
$batch_ids = '';
$count = count($_POST['UniversalElectives']['batch']);
//set the submitted values
$model->attributes=$_POST['UniversalElectives'];
$model->offered_for = $POST['UniversalElectives']['offered_for'];
for($i=0;$i<=$count;$i++)
{
if($i!=0 and $i!=$count){
$batch_ids = $batch_ids.',';
}
$batch_ids = $batch_ids.$_POST['UniversalElectives']['batch'][$i];
}
$model->batch = $batch_ids;
$model->created_by = Yii::app()->user->id;
$model->created_at = date('Y-m-d h:i:s');
//return the JSON result to provide feedback.
if($model->save(false)){
echo json_encode(array('success'=>true,'id'=>$model->primaryKey) );
exit;
} else
{
echo json_encode(array('success'=>false));
exit;
}
}
}