Commit cf5e3705 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

support min and max for checkboxes in webform_validation module

parent fef38e6d
......@@ -865,9 +865,30 @@ function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
$variables = array(
'message' => '!title field is required.',
'placeholders' => array('!title' => $title),
if ($message == '') {
if ($min == 1 && $max == 99) {
$message = '!title field is required.';
$placeholders = array('!title' => $title);
}
if ($min == 0 && $max != 99) {
$message = 'You can select no more than !max values for !title.';
$placeholders = array('!title' => $title, '!max' => $max);
}
if ($min != 1 && $max == 99) {
$message = 'You must select at least !min values for !title.';
$placeholders = array('!title' => $title, '!min' => $min);
}
if ($min > 0 && $max != 99) {
$message = 'You must select between !min and !max values for !title.';
$placeholders = array('!title' => $title, '!min' => $min, '!max' => $max);
}
}
else {
$placeholders = array();
}
$variables = array(
'message' => $message,
'placeholders' => $placeholders,
'error_type' => 'required',
'element_name' => $name
);
......
......@@ -167,12 +167,36 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
break;
case 'select_min':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength($component['element_name'], $component['element_title'], $webform_validation_rule['data'], '', $js_rules);
$parent_keys = webform_component_parent_keys($form['#node'], $component);
$form_element = $form['submitted'];
foreach($parent_keys as $parent_key) {
$form_element = $form_element[$parent_key];
}
if (in_array($form_element['#type'], array('checkbox', 'checkboxes'))) {
$checkboxrules[$component['element_name']]['title'] = $component['element_title'];
$checkboxrules[$component['element_name']]['id'] = '#' . $form_element['#id'];
$checkboxrules[$component['element_name']]['min'] = $webform_validation_rule['data'];
}
else {
_clientside_validation_set_minmaxlength($component['element_name'], $component['element_title'], $webform_validation_rule['data'], '', $js_rules);
}
}
break;
case 'select_max':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength($component['element_name'], $component['element_title'], '', $webform_validation_rule['data'], $js_rules);
$parent_keys = webform_component_parent_keys($form['#node'], $component);
$form_element = $form['submitted'];
foreach($parent_keys as $parent_key) {
$form_element = $form_element[$parent_key];
}
if (in_array($form_element['#type'], array('checkbox', 'checkboxes'))) {
$checkboxrules[$component['element_name']]['title'] = $component['element_title'];
$checkboxrules[$component['element_name']]['id'] = '#' . $form_element['#id'];
$checkboxrules[$component['element_name']]['max'] = $webform_validation_rule['data'];
}
else {
_clientside_validation_set_minmaxlength($component['element_name'], $component['element_title'], '', $webform_validation_rule['data'], $js_rules);
}
}
break;
case 'select_exact':
......@@ -202,6 +226,15 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
break;
}
}
foreach ($checkboxrules as $name => $args) {
if (!isset($args['min'])) {
$args['min'] = 1;
}
if (!isset($args['max'])) {
$args['max'] = 99;
}
_clientside_validation_set_checkboxgroup_minmax($name, $args['title'], $args['id'], $js_rules, '', $args['min'], $args['max']);
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment