I am working with ajax crud in Yii. I have a radiobuttonlist in the form. I get 'cannot be blank' error even if it is selected. Please help me find the problem.
In the ajax_form the code is:
$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'); ?>
In the model, the rules are:
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'),
);
}
In the controller:
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;
}
}
}