Validate Or Limit Number Of Checkboxes Selected In Ccheckboxcolumn

Is there any way I can limit the number of checkboxes selected in ccheckboxcolumn to the exact number I want?

I know that selectableRows=2 can allow you to select more than 2 rows. But I want to be able to specify the exact number of rows to select (eg. can select only 10 rows and nothing more than it).

Or is there any way to validate the ccheckboxcolumn such that I can only press the "submit" button if I select less than specified number of row / checkbox (eg. can press submit button only when I select less than 10 rows)?

use js . add listener to the checkbox or submit button .

for number of checked limit : onchange can listen that , every time checkbox checked or unchecked you should get the all checked checkbox and count them … if out of the number bound then do some thing (rallback check action! it 's not hard and give a msgBox tell user what’s happened !)

the submit ?? listen the submit event and then … you should know hot to do that :D

Thanks. But can you show an example of it?

I’m new to yii and have not use listeners before so I’m not sure where and how to attach the listener to the ccheckboxcolumn and ajax submit button.

just as normal jquery usage(use firebug to see the structure of your generated view file in browser ):




 //  when form submit you can judge here , return false will prevent the submit !

$('form').submit(function(){

             //...  submit handle here

	return false;

});


$(":checkbox","checkbox-column").on('change',function(){


  .....

});




I still don’t understand how I can add a listener to the ccheckboxcolumn to do client validation to limit the number of checkbox selected.

I can visualize what will happen by adding listener to each individual checkbox to count the number of checkboxes selected and limit the selection but I can’t see visualize how it’s like to add a listener to a ccheckboxcolumn.

The main thing here is I don’t know how to add a listener to a ccheckboxcolumn and I can’t find sample codes or tutorials to start with.

I’ve been stuck in this for a very long time.

Can anyone help?

Dear Friend

I hope the following may be helpful.

On Server Side.

In your model( eg Model:TestForm , attribute:yourCheckBoxAtrribute)

TestForm.php




public function rules()

{

	return array(

	 .................................................

         ...............................................

	 array('yourCheckBoxAtrribute','validateCheckBox'),

                             

		);

}


public function validateCheckBox($attribute,$params){

		

		if(count($this->yourCheckBoxAttribute)>2)

		   $this->addError($attribute,'Kindly restrict yourself for less than two options');

		

		}



On Client Side.

Create a div element to display the error message below the checkBox.




<div  id="er"></div>



Register this script at the end in your view/_form.php




Yii::app()->clientScript->registerScript('someName','$("#test-form").submit(  //id for your form

	

	function() {

	    var selectedFields=$("#test-form").serializeArray();

	    var n=0;

	    $.each(selectedFields,function(index,field)

               {

                  if(field.name=="TestForm[yourCheckBoxAttribute][]")   n++; 

               }

            );


	   if(n>2) 

               {

                  $("#er").addClass("flash-error").html("select only two items");


                  return false;

              }

	 

	}

	)');




It is going to count the number of options selected. If it is exceedind 2, it is going to call "return false".

That means it is going to stop the normal behavior of submit button.

At the same time it is going to add the class "flas-error" to our error message div element and also the error text.

Regards.

Thanks seenivasan.

Can I check with you, for the "yourCheckBoxAtrribute", is it the ccheckboxcolumn id of the cgridview?

Because I tried replacing "yourCheckBoxAtrribute" as the ccheckboxcolumn id (eg. autoId) but it comes out an error at the model side "property autoId is not defined". I think is because the "autoId" is defined at the view inside cgridview ccheckboxcolumn id and cannot be found inside the model.

My cgridview ccheckboxcolumn is populated based on the combination of 2 tables (i.e. 2 models - Project and ProjectSignup) as shown in the codes below. So should I place the model codes inside Project model or ProjectSignup model?




$dataProvider=new CActiveDataProvider('ProjectSignup', array(

	'criteria'=>array(

		'condition'=>'t.project_id='.$proj_id,

		'join'=>'LEFT JOIN project ON t.project_id=project.project_id LEFT JOIN user ON t.user_id=user.id',		

		'order'=>'t.signup_status desc',

		),

));

	



Dear Friend

Sorry for very late response.

My solution presented earlier has no relevance to your problem.

Kindly check whether the following is helpful.

Render a button below the grid at admin.php




<?php echo CHtml::button('Select',array('id'=>'butt'));?>



Register the following script at the bottom of admin.php




Yii::app()->clientScript->registerSCript('restrictSelection','$("#butt").click(function() {


    var arr=$("#brand-grid").yiiGridView("getChecked","brand-grid_c4");


    if(arr.length>5) 

     {

         alert("select only 5 items or less");

         return false;

     }


    else console.log(arr);


    });');

?>






//variable arr is array containing pk(id) values of each row. You can do anything you want.



brand denotes model Brand (in my case).

brand-grid_c4 denotes column id of checkbox column(5th column in my case).

Regards.