Cactiveform Checkboxlist

i have a problem with checkboxlist in CActiveform… im trying to save values as array to all boxes checked… like.

1=>check

2=>check

3=>uncheck

4=>check

result=array(1,2,4);

this is my form code


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'caracterizacion-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'parametro'); ?>

		<?php echo $form->textField($model,'parametro',array('size'=>60,'maxlength'=>256)); ?>

		<?php echo $form->error($model,'parametro'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'decreto_id'); ?>

		<?php echo $form->checkBoxList($model,'decreto_id', CHtml::listData(Decreto::model()->findAll(),'id','ndecreto')); ?>

		<?php echo $form->error($model,'decreto_id'); ?>

	</div>

	

	




	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

but in this code it gives me this error


Please fix the following input errors:


    Decreto is invalid.



please help me

Hi there.

Take a look at this shgn.ir/checkboxlist codes, maybe help you.

this page seems to be arabic… i’d like to understand what the programmer says. for now the code its a bit confusing for me. ill keep it in mind to check it back later if i dont find another solution… thanks

No, It was in Persian. I put that link just for the php codes, maybe, helpful for you.:mellow:

Look at your Decreto model. There should be some validation rules applied to decreto_id.

One of them thinks that your input is invalid.

Also check how the input array is actually being stored. I suppose there should be some sort of setter or beforeValidate hook.

and how can i set the result form?.. i mean how is the output data?

does anyone know?.. its just that i have to make a table with alot… alot data (lets just say like 200 colums), and i wouldnt want to do it manually on the DB directly… what im trying to do is this:

the seller has to set (like 200) data on the page, this could change in the future by the seller.

when its making an information that needs to have this (like 200 more or less), but he wont choose all of them so i thought it would work with checkboxlist…

all i wanted to do is to make an output that will save in DB like (1,2,6,9,12,13,15… etc…), so when its called in another site of the page it will print information according to thoses ids choosen in the checkbox list… it would be so exhausting just to put them all in DB directly.

does anyone know? if not. thanks for your help

It’s not quite clear what are you trying to do.

I suppose you need MANY_MANY relation (Seller <-> SellerGoods <-> Goods).

Hello,

I’m facing an issue with Cactiveform Checkboxlist since few days…

I hope some of you can help me.

Here is the checkBoxList of my _form.php view :




        <div class="row">

		<?php	

		echo $form->labelEx($user_model,'Current authorized users</br><i>(Please check the ones you want to delete)</i>');

		echo $form->checkBoxList($user_model,'username', $checkbox_username, array('template'=>'<ul>{label} {input}</ul>','labelOptions'=>array('style'=>'display:inline'),));	

		echo $form->error($user_model,'username');			

		?>

	</div>



The problem was that the attribute ‘username’ was not in my current $model (called BoxModel) but in another model called UserModel. Also the tables “boxs” and “users” are not directly linked by a join table (so I haven’t any relations written).

So the variable $user_model refers to the model UserModel.

$checkbox_username is an array and contains users names.

Here is the concerned part of my controller boxController:




public function actionUpdate($id)

{

	$model=$this->loadModel($id);

	$user_model=new User;


	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model, $user_model);


	if(isset($_POST['BoxModel']) && isset($_POST['UserModel']))

	{

 Yii::log('POST USER : '.json_encode($_POST['UserModel']), 'warning');


	        $var_group=array();		

	        foreach($_POST['BoxModel'] as $value){

	          $var_group[]=$value['groupvb'];

	        }

	        $model->update_group($id,$var_group[1]);			

	        $model->attributes=$_POST['BoxModel'];


                $this->redirect(array('view','id'=>$model->id));

	}

	

	if(isset($_GET['id'])){

		$this_groupid = $model->get_group_vb_id($_GET['id']);

		$checkbox_username = $model->get_array_user($this_groupid);			

	}

	

	$userid = Yii::app()->user->id;

	if(isset($userid)){

		$groupid = $model->get_group_user_id($userid);

	}

	

	$this->render('update',array(

		'model'=>$model,

		'user_model'=>$user_model,

		'checkbox_username'=>$checkbox_username,

	));

}



The problem now is with the content of my $_POST[‘UserModel’].

The result of my log is :

if I check "admin" and submit my form : {"username":["0"]} //the first checkbox

if I check "demo" and submit my form : {"username":["1"]}, //the second checkbox

if I check "smarc" and submit my form : {"username":["2"]}, //the third checkbox

And I would like to have :

{"username":["admin"]}

{"username":["demo"]}

{"username":["smarc"]}

As if I have done


echo $form->checkBoxList($user_model, 'username', array('admin'=>'admin', 'demo'=>'demo', 'smarc'=>'smarc'));

How can I do that? What is wrong with my code?

I have tried several alternatives found in forums, or this wiki but I can’t find a solution to my problem =(

Thanks in advance for any help or suggestions!

Nobody have an idea? :(

First you need to find your pre-selected categories like -


$criteria = new CDbCriteria();

$criteria->select = 'category_id as id';

$criteria->condition = 'userid = :userid';

$criteria->params = array(':userid' => Yii::app()->user->id);


//store pre-selected id into variable  - $selected_keys

$selected_keys = array_keys(CHtml::listData(MyCategory::model()->findAll($criteria), 'id', 'id'));

Now generate whole category list, like -


$list = CHtml::listData(Categories::model()->findAll(array('order'=>'id')), 'id', 'category_name');


//htmlOptions for class and others elements

$htmlOptions = array('template' => '{input}{label}', 'separator'=>'', 'class'=>'in-checkbox', 'multiple'=>true, 'checked'=>'checked');

View part -


<?php echo $form->labelEx($model, 'Category', array('class'=>'col-md-3 control-label')); ?>

<?php $model->Category = $selected_keys; //assign pre-selected list to Category list

      echo $form->checkBoxList($model, 'Category', $list, $htmlOptions); ?>

<?php echo $form->error($model, 'Category'); ?>

Try this, work great…