Here’s the scenario: I was working on the form with textfield, a button, a checkboxlist(which will be filled with textbox value). But the problem is when enter something in the textfield, the entry is added in the checkboxlist, but value is not showing(meaning it’s a checkbox without name/label). I tested it with firebug. it is showing the value of textfield in the POST. But in the model it is showing blank. Here is the code.
View file
============================================
<?php /** @var BootActiveForm $form */
$form = $this->beginWidget(‘bootstrap.widgets.TbActiveForm’, array(
'id'=>'testForm',
'htmlOptions'=>array('class'=>'well'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->textFieldRow($model, ‘text1’, array(‘class’=>‘span3’)); ?><br>
<?php echo CHtml::button(‘check’, array(‘submit’ => array(‘test/gridtest1’))); ?>
<div class="row-fluid">
<?php echo CHtml::activeCheckBoxList($model,'wh1',$list,array('style'=>'float:left')); ?>
</div>
//<?php foreach ($list as $value) {
// echo $value."<->";
//} ?>
<?php $this->endWidget(); ?>
============================================
Model file
============================================
<?php
/**
-
This is the model class for table "actions".
-
The followings are the available columns in table ‘actions’:
-
@property integer $id
-
@property string $action
*/
class Actions extends CActiveRecord
{
public $text1;
public $wh1;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'actions';
}
/**
* @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('action', 'required'),
//array('action', 'length', 'max'=>50),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, action', 'safe', 'on'=>'search'),
);
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function addAction()
{
if($this->text1 === "")
{
$this->addError('text1','errororororororororororor....');
}
else
{
//$sql="insert into actions(action) values('".$this->text1."');";
$sql = "INSERT INTO `actions`(`action`) VALUES ('$this->text1')";
$insert = Yii::app()->db->createCommand($sql);
if($insert->execute() == 1){
$cmd= Yii::app()->db->createCommand();
$cmd->select="*";
$cmd->from="actions";
$data=$cmd->query();
$list = array();
foreach($data as $checkbox)
{
$list[]= $checkbox["action"];
}
return $list;
}
else {
echo "Error occured......";
}
}
}
}
==========================================================================
Controller code
==========================================================================
<?php
class TestController extends Controller
{
public $defaultAction = 'gridtest1';
public function actiongridtest1()
{
$model = new Actions();
$data = $model->addAction();
$msg ="";
$this->render('gridtest1',array('model'=>$model,'msg'=>$msg,'list'=>$data));
}
}