Commit 83aaeb35 authored by jelles's avatar jelles
Browse files

Min, max & range for decimal fields with ',' as decimal separator

parent d2bdfd8e
......@@ -823,6 +823,21 @@
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
});
jQuery.validator.addMethod("min_comma", function(value, element, param) {
var real_val = Number(value.replace(',', '.'));
return this.optional(element) || real_val >= param;
});
jQuery.validator.addMethod("max_comma", function(value, element, param) {
var real_val = Number(value.replace(',', '.'));
return this.optional(element) || real_val <= param;
});
jQuery.validator.addMethod("range_comma", function(value, element, param) {
var real_val = Number(value.replace(',', '.'));
return this.optional(element) || (real_val >= param[0] && real_val <= param[1]);
});
// Min a and maximum b checkboxes from a group
jQuery.validator.addMethod("checkboxgroupminmax", function(value, element, param) {
var amountChecked = $(param[2]).find('input:checked').length;
......
......@@ -580,64 +580,67 @@ function _clientside_validation_set_number_decimal($name, $title, $decimalpoint,
/**
* Set validation rule for fields with a minimum and/or a maximum value.
*/
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '', $decimal_separator = '.') {
$title = _clientside_validation_set_title($title);
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['range'] = array($min, $max);
$rule = $decimal_separator == '.' ? 'range' : 'range_comma';
$js_rules[$name][$rule] = array($min, $max);
if (empty($message)) {
$variables = array(
'message' => '!title field has to be between !min and !max.',
'placeholders' => array('!title' => $title, '!min' => $min, '!max' => $max),
'error_type' => 'range',
'error_type' => $rule,
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'range',
'error_type' => $rule,
'element_name' => $name,
);
}
$js_rules[$name]['messages']['range'] = theme('clientside_error', $variables);
$js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$js_rules[$name]['min'] = $min;
$rule = $decimal_separator == '.' ? 'min' : 'min_comma';
$js_rules[$name][$rule] = $min;
if (empty($message)) {
$variables = array(
'message' => '!title field has to be greater than !min.',
'placeholders' => array('!title' => $title, '!min' => $min),
'error_type' => 'min',
'error_type' => $rule,
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'min',
'error_type' => $rule,
'element_name' => $name,
);
}
$js_rules[$name]['messages']['min'] = theme('clientside_error', $variables);
$js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$js_rules[$name]['max'] = $max;
$rule = $decimal_separator == '.' ? 'max' : 'max_comma';
$js_rules[$name][$rule] = $max;
if (empty($message)) {
$variables = array(
'message' => '!title field has to be smaller than !max.',
'placeholders' => array('!title' => $title, '!max' => $max),
'error_type' => 'max',
'error_type' => $rule,
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'max',
'error_type' => $rule,
'element_name' => $name,
);
}
$js_rules[$name]['messages']['max'] = theme('clientside_error', $variables);
$js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
}
}
......
......@@ -107,7 +107,7 @@ function clientside_validation_regular($form_id, $element, &$js_rules, $form_sta
$max = $instance['settings']['max'];
}
if ($min !== '' || $max !== '') {
_clientside_validation_set_minmax($el_name, $el_title, $min, $max, $js_rules);
_clientside_validation_set_minmax($el_name, $el_title, $min, $max, $js_rules, '', $field['settings']['decimal_separator']);
}
if (isset($field['settings']['decimal_separator'])) {
......
Markdown is supported
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