Commit 3a047994 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

added support for HTML5 range element. see also #1174646

parent bd9bb8a0
......@@ -270,15 +270,15 @@ function _clientside_validation_set_number_decimal($name, $title, $decimalpoint,
*/
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
if (isset($min) && $min != '' && isset($max) && $max != '') {
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['range'] = array($min, $max);
$js_rules[$name]['messages']['range'] = (empty($message)) ? t('!name field has to be between !min and !max.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
}
elseif (isset($min) && $min != '') {
elseif (isset($min) && $min !== '') {
$js_rules[$name]['min'] = $min;
$js_rules[$name]['messages']['min'] = (empty($message)) ? t('!name field has to be greater than !min.', array('!name' => $title, '!min' => $min)) : $message;
}
elseif (isset($max) && $max != '') {
elseif (isset($max) && $max !== '') {
$js_rules[$name]['max'] = $max;
$js_rules[$name]['messages']['max'] = (empty($message)) ? t('!name field has to be smaller than !max.', array('!name' => $title, '!max' => $max)) : $message;
}
......@@ -292,15 +292,15 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
if ($max == 1) {
$max = '';
}
if (isset($min) && $min != '' && isset($max) && $max != '') {
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['rangelength'] = array($min, $max);
$js_rules[$name]['messages']['rangelength'] = (empty($message)) ? t('!name field has to have between !min and !max values.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
}
elseif (isset($min) && $min != '') {
elseif (isset($min) && $min !== '') {
$js_rules[$name]['minlength'] = $min;
$js_rules[$name]['messages']['minlength'] = (empty($message)) ? t('!name field has to have minimal !min values.', array('!name' => $title, '!min' => $min)) : $message;
}
elseif (isset($max) && $max != '') {
elseif (isset($max) && $max !== '') {
$js_rules[$name]['maxlength'] = $max;
$js_rules[$name]['messages']['maxlength'] = (empty($message)) ? t('!name field has to have maximum !max values.', array('!name' => $title, '!max' => $max)) : $message;
}
......
......@@ -20,7 +20,7 @@ function clientside_validation_html5_after_build_recurse($form_id, &$form, &$for
foreach ($children as $index => $item) {
$element = &$form[$item];
$types = array(
'number', 'url', 'email',
'number', 'url', 'email', 'range',
);
if (isset($element['#type']) && in_array($element['#type'], $types)) {
clientside_validation_html5($form_id, $element, $js_rules);
......@@ -45,9 +45,16 @@ function clientside_validation_html5($form_id, $element, &$js_rules) {
$required_error = isset($element['#required_error']) ? $element['#required_error'] : '';
_clientside_validation_set_required($el_name, $el_title, $element["#required"], $js_rules, $required_error);
switch ($element['#type']) {
case 'range':
$default_min = 0;
$default_max = 100;
case 'number':
$min = isset($element['#min']) ? $element['#min'] : '';
$max = isset($element['#max']) ? $element['#max'] : '';
if (!isset($default_min)) {
$default_min = '';
$default_max = '';
}
$min = isset($element['#min']) ? $element['#min'] : $default_min;
$max = isset($element['#max']) ? $element['#max'] : $default_max;
$step = isset($element['#step']) ? $element['#step'] : 1;
$is_decimal = $step != 'any' && (floor($step) != $step || floor($min) != $step);
if ($is_decimal) {
......@@ -77,15 +84,15 @@ function _clientside_validation_set_minmax_html5($name, $title, $min, $max, $ste
if( $step == 'any') {
_clientside_validation_set_minmax($name, $title, $min, $max, $js_rules, $message);
}
elseif (isset($min) && $min != '' && isset($max) && $max != '') {
elseif (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['Html5Range'] = array($min, $max, $step);
$js_rules[$name]['messages']['Html5Range'] = (empty($message)) ? t('!name field has to be greater than !min with steps of !step and smaller than !max.', array('!name' => $title, '!min' => $min, '!max' => $max, '!step' => $step)) : $message;
}
elseif (isset($min) && $min != '') {
elseif (isset($min) && $min !== '') {
$js_rules[$name]['Html5Min'] = array($min, $step);
$js_rules[$name]['messages']['Html5Min'] = (empty($message)) ? t('!name field has to be greater than !min with steps of !step.', array('!name' => $title, '!min' => $min, '!step' => $step)) : $message;
}
elseif (isset($max) && $max != '') {
elseif (isset($max) && $max !== '') {
$js_rules[$name]['Html5Max'] = array($max, $step);
$js_rules[$name]['messages']['Html5Max'] = (empty($message)) ? t('!name field has to be smaller than !max and must be dividable by !step.', array('!name' => $title, '!max' => $max, '!step' => $step)) : $message;
}
......
......@@ -303,7 +303,7 @@ $.extend($.validator, {
validator.settings[eventType] && validator.settings[eventType].call(validator, this[0] );
}
$(this.currentForm)
.validateDelegate(":text, [type='number'], [type='url'], [type='email'], :password, :file, select, textarea", "focusin focusout keyup", delegate)
.validateDelegate(":text, [type='number'], [type='url'], [type='email'], [type='range'], :password, :file, select, textarea", "focusin focusout keyup", delegate)
.validateDelegate(":radio, :checkbox, select, option", "click", delegate);
if (this.settings.invalidHandler)
......
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