how to validate group of checkboxes?

Hi!

I have an form, where the user can "check"/click some checkboxes. The checkboxes belong to one group.

That means, if the user submits the form, $_POST includes the values of the checkboxes as an array.

Example: an form offers dozen of colors as an checkbox-group

[ ]red [x]green [x]blue [ ]yellow

If the form is submitted to the server, $_POST["COLOR"] includes the array of checked boxes

array{0=>‘green’,1=>‘blue’}

To avoid X-side-scripting, hacking-attacks or something else, i like to validate the submitted results.

For validation it’s not relevant which box is checked. But it’s important, that only the allowed checkbox-values are been progressed by the action.

So the model includes an array with valide values. ($_validColors=array(‘red’=>‘red’,‘blue’=>‘blue’,…) )

For Validation i tried the following rule:


array('COLOR','range','in','range'=>array_keys($this->$_validColors)),

(is this the right way to use that kind of validation?)

But it seems not to be working - no error validation occours. It lookes like the submitted values will be ignored?!

Maybe anyone has an idea?

Thank you!

Has nobody any idea, hat to make this in a right way?

One solution that should work for your needs is to create a custom validation method and check the posted array by hand.

The result of searching the forum with keywords custom validation gives you a lot of examples.

Guide - Declaring Validation Rules

Hi, could someone please point me to a full code example of group of checkbox (CActiveForm:checkBoxList) validation (including client-side)?

I’m not having much luck with client-slide validation where at least one of the checkboxes in a group has to be selected.

Would be great if there were more code examples in the docs…

Thanks!

"Your search - custom validation - did not match any documents. Suggestions: Make sure all words are spelled correctly. Try different keywords. Try more general keywords."! :]

But that’s just a joke, as forum search is not working very well lately…