How to use addRule with a group of checkbox in a form?

How to use addRule with a group of checkbox in a form?

by Rodrigo Sampaio Primo -
Number of replies: 2
Hi, I have a multiple checkbox group inside a form and I would like to verify if at least one of the checkbox is marked. Simply using addRule to the group doesn't work. Any ideas?

I've tried the following code:

$checkboxarray=array();
foreach ($contribution_types as $type) {
$checkboxarray[] =& $mform->createElement('advcheckbox', 'contribtype' . $type->id, '', $type->typename);
}
$mform->addGroup($checkboxarray, 'contribtype', get_string('contribtype', 'forum'), array(' '), false);
if ($forum->contribmandatory) {
$mform->addRule('contribtype', get_string('required'), 'required', null, 'client');
}

Thanks, Rodrigo.


Average of ratings: -
In reply to Rodrigo Sampaio Primo

Re: How to use addRule with a group of checkbox in a form?

by Ann Adamcik -
You might try addGroupRule instead of addRule. See the function definition in lib/formslib.php.
In reply to Ann Adamcik

Re: How to use addRule with a group of checkbox in a form?

by Deleted user -
Hi,

I'm stuck in a similar situation, but the 'addGroupRule' solution wouldn't help me whatsoever in my problem.

I've got few groups of form fields, each group contains three form elements.

What I would need is the 'addFormRule' solution. But I haven't succeeded in making it work.
I need each group checked individually and depending on the situation of the other form element groups, so that's why 'addGroupRule' solution doesn't suit me in this case.

For testing, I've created a function with the code to be executed as a rule, and I add this function to the form group:

...
$mform->addFormRule('name_of_the_function');
...
function name_of_the_function($element)
...

But it seems it doesn't work. Do you have an idea what I'm doing wrong?

Thanks in advance.