By 3cwebdev on
In Drupal 5.x: I am stuck at trying to validate an admin form that uses checkboxes for a selection. I need to make sure at least one checkbox is ticked. How do I validate this?
Thanks~
In Drupal 5.x: I am stuck at trying to validate an admin form that uses checkboxes for a selection. I need to make sure at least one checkbox is ticked. How do I validate this?
Thanks~
Comments
BMP- Any way to validate that at least one checkbox was selected
BUMP- Any way to validate that at least one checkbox was selected?
The Cosmic Gift | Complete Computer Care | Team Hope
Unchecked boxes return 0
Example taken from the Forms API:
At your form validation function when you check the
$form_valuesarray you will see:Array ( [test] => Array ( [moderate] => moderate [sticky] => sticky [status] => 0 [promote] => 0 [revision] => 0 )which means 0 for unchecked boxes and the key of the options array for checked ones. So you may use:
to get the number of ticked boxes and require it to be greater than 0.
Need more help please...
I appreciate your reply but I'm afraid I am still stuck with this. I have tried many combos with the example you provided and it still does not work for me. I'm sure the problem is in the fact that I don't really understand what the last parts of the validation line does ... create_function("$box_value", "return $box_value != 0")))
Here is my latest attempt:
I've cruised right through all the other parts of my module development, it's kinda' annoying to be stuck on what seems like it should be such a simple task. Any more help would be greatly appreciated.
The Cosmic Gift | Complete Computer Care | Team Hope
It creates an anonymous function
to be used by php's array_filter function. If you don't know much about php or callback functions, don't worry about that part.
First make sure you are using the correct function name:
If you can see that error message when you send the form than you are OK. Otherwise, post the form function here, so I can tell you the appropriate function name.
Next do this
Let me know if this didn't work.
Thanks again, but still not working...
I verified that the function name was correct with the above test and it did show form validation the error message 'This form would never validate.'
When I used the code you supplied for the actual validation I get this error:
****************************************************************************************
# warning: array_filter(): The second argument, '', should be a valid callback in /rootdir/sites/all/modules/bibleplans/bibleplans.module on line 138.
# You must select at least one box
****************************************************************************************
...which is the same as when I tried with my first attempt.
Perhaps I'm missing something in the way that I'm building/using the form element?
The Cosmic Gift | Complete Computer Care | Team Hope
Sorry, my mistake
I wrote the code over the top of my head and made the mistake of using double quotes at create_function's parameters. Try this:
This is a shorter way of doing this:
Geeez! Still not working
Not for lack of effort, I still can not get this simple function to work properly!
I appreciate your help with this but after three days of trying I still can't validate the checkboxes.
I used both long and short versions of the code supplied, I ended up using the long version because it made sense to me.
Here is the current incarnation:
No matter how I implement the code the checkboxes always fail the validation. Could the problem be in the way that I build the checkbox options array?
Using the Variable editor, this is what it shows the bibleplans_plans array to be:
... which is what I expected it to be with the first of the two checkboxes options ticked. When I made a $test string to append to it the value of $boxvalue on every iteration through the loop, this is the output:
chronological
0"
So the problem is that the ($boxvalue !=0) is not evaluating correctly. I tried ..!='0', with the same result. The really weird thing is that even if I just make an always true statement if(1){.... then the evaluation function doesn't even appear to run (the test I put in the if statement and the evaluation error never fire).
After so much time invested I am at a total loss right now as to what is wrong. Any ideas, or is there a functioning example to look at somewhere?
The Cosmic Gift | Complete Computer Care | Team Hope
That's weird
Did you try making the first parameter of form_set_error empty string, like:
May be you are giving the wrong name. Other than that I don't know what is wrong. The array seems correct. What happens when you check the value of
$number_of_checked_boxesafter the foreach loop is completed? You can also check if you are going inside the last if loop like this:I give up....
I spent several more hours on it tonight. Yes, I did try making the first parameter of form_set_error empty string. I must of tried every conceivable test and combination to get this thing to work. I thought I had it at one point by making the comparison a single quoted string ... if ($check != '0'){ ... that was the only time it appeared to work, but the next attempt it failed again.
Short of someone actually taking the time to look at the module I don't know what else to do. membeci, I really appreciate all of your help and patience....
The Cosmic Gift | Complete Computer Care | Team Hope
';' missing in create_function 'code' argument
This should work:
And also quotation marks surrounding the zero number
It works just putting the zero between quotation marks.
I mean, the second argument of the create_function should be: 'return $box_value != "0";'
Remember the array value brought by $form_values is not an int, its a string.
The code that should definitively work:
After this, why don't we just set to TRUE the '#required' when building the form?