Commit 5142b00f authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1316678 by mropanen: Fixed Field validation not working.

parent 0dc0b32e
......@@ -469,9 +469,21 @@
}, jQuery.format(''));
jQuery.validator.addMethod("specificVals", function(value, element, param){
for (var i in value){
for (var i in value) {
if(param.indexOf(value[i]) == -1) {
return false;
return false;
}
}
return true;
});
jQuery.validator.addMethod("blacklist", function(value, element, param) {
if (typeof(value) !== 'object') {
value = value.split(' ');
}
for (var i in value) {
if(param.indexOf(value[i]) !== -1) {
return false;
}
}
return true;
......@@ -524,61 +536,43 @@
}, jQuery.format('The value does not match the expected format.'));
jQuery.validator.addMethod("daterange", function(value, element, param) {
//Assume [month], [day], and [year] ??
var dayelem, monthelem, yearelem, name;
if ($(element).attr('name').indexOf('[day]') > 0) {
dayelem = $(element);
name = dayelem.attr('name').replace('[day]', '');
monthelem = $("[name='" + name + "[month]']");
yearelem = $("[name='" + name + "[year]']");
}
else if ($(element).attr('name').indexOf('[month]') > 0) {
monthelem = $(element);
name = monthelem.attr('name').replace('[month]', '');
dayelem = $("[name='" + name + "[day]']");
yearelem = $("[name='" + name + "[year]']");
}
else if ($(element).attr('name').indexOf('[year]') > 0) {
yearelem = $(element);
name = yearelem.attr('name').replace('[year]', '');
dayelem = $("[name='" + name + "[day]']");
monthelem = $("[name='" + name + "[month]']");
jQuery.validator.addMethod("rangewords", function(value, element, param) {
return this.optional(element) || (param[0] <= jQuery.trim(value).split(' ').length && value.split(' ').length <= param[1]);
}, jQuery.format('The value must be between {0} and {1} words long'));
}
jQuery.validator.addMethod("minwords", function(value, element, param) {
return this.optional(element) || param <= jQuery.trim(value).split(' ').length;
}, jQuery.format('The value must be more than {0} words long'));
jQuery.validator.addMethod("maxwords", function(value, element, param) {
return this.optional(element) || jQuery.trim(value).split(' ').length <= param;
}, jQuery.format('The value must be less than {0} words long'));
if (parseInt(yearelem.val(), 10) < parseInt(param[0][0], 10)) {
return false;
}
else if (parseInt(yearelem.val(), 10) == parseInt(param[0][0], 10)){
if (parseInt(monthelem.val(), 10) < parseInt(param[0][1])){
return false;
}
else if (parseInt(monthelem.val(), 10) == parseInt(param[0][1], 10)){
if(parseInt(dayelem.val(), 10) < parseInt(param[0][2], 10)) {
return false;
}
}
}
jQuery.validator.addMethod("plaintext", function(value, element, param){
return this.optional(element) || (value == strip_tags(value, param));
});
if (parseInt(yearelem.val(), 10) > parseInt(param[1][0], 10)) {
return false;
// Unique values
jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
var target = $(param).unbind(".validate-notEqualTo").bind("blur.validate-notEqualTo", function() {
$(element).valid();
});
return value != target.val();
}, jQuery.format('Please don\'t enter the same value again.'));
jQuery.validator.addMethod("regexMatch", function(value, element, param) {
if (this.optional(element) && value == '') {
return this.optional(element);
}
else if (parseInt(yearelem.val(), 10) == parseInt(param[1][0], 10)){
if (parseInt(monthelem.val(), 10) > parseInt(param[1][1])){
return false;
}
else if (parseInt(monthelem.val(), 10) == parseInt(param[1][1], 10)){
if(parseInt(dayelem.val(), 10) > parseInt(param[1][2], 10)) {
return false;
}
else {
var regexp = new RegExp(param[0], param[1]);
if(regexp.test(value)){
return true;
}
return false;
}
yearelem.removeClass('error');
monthelem.removeClass('error');
dayelem.removeClass('error');
return true;
});
}, jQuery.format('The value does not match the expected format.'));
jQuery.validator.addMethod("datemin", function(value, element, param) {
//Assume [month], [day], and [year] ??
......@@ -704,6 +698,15 @@
//Allow other modules to add more rules:
jQuery.event.trigger('clientsideValidationAddCustomRules');
function strip_tags (input, allowed) {
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
}
})(jQuery);
......@@ -323,15 +323,24 @@ function _clientside_validation_set_required($name, $title, $required, &$js_rule
/**
* Set validation rule for number fields.
*/
function _clientside_validation_set_number($name, $title, &$js_rules) {
function _clientside_validation_set_number($name, $title, &$js_rules, $message = '') {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$js_rules[$name]['digits_negative'] = TRUE;
$variables = array(
'message' => '!title field accepts only numbers.',
'placeholders' => array('!title' => $title),
'error_type' => 'number',
'element_name' => $name,
);
if (empty($message)) {
$variables = array(
'message' => '!title field accepts only numbers.',
'placeholders' => array('!title' => $title),
'error_type' => 'number',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'number',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['digits_negative'] = theme('clientside_error', $variables);
}
......@@ -554,6 +563,94 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
}
}
/**
* Set validation rule for fields with a minimum and/or maximum amount of words.
*/
function _clientside_validation_set_minmax_words($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 !== '') {
$js_rules[$name]['rangewords'] = array($min, $max);
if (empty($message)) {
$variables = array(
'message' => '!title field has to have between !min and !max words.',
'placeholders' => array('!title' => $title, '!min' => $min, '!max' => $max),
'error_type' => 'rangewords',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'rangewords',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['rangewords'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$js_rules[$name]['minwords'] = $min;
if (empty($message)) {
$variables = array(
'message' => '!title field has to have minimal !min words.',
'placeholders' => array('!title' => $title, '!min' => $min),
'error_type' => 'minwords',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'minwords',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['minwords'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$js_rules[$name]['maxwords'] = $max;
if (empty($message)) {
$variables = array(
'message' => '!title field has to have maximum !max words.',
'placeholders' => array('!title' => $title, '!max' => $max),
'error_type' => 'maxwords',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'maxwords',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['maxwords'] = theme('clientside_error', $variables);
}
}
/**
* Set validation rule for plain text fields
*/
function _clientside_validation_set_plain_text($name, $title, $allowed_tags, &$js_rules, $message = '') {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$js_rules[$name]['plaintext'] = empty($allowed_tags) ? '' : $allowed_tags;
if (empty($message)) {
$variables = array(
'message' => (empty($allowed_tags)) ? '!title field can not contain any HTML tags' : '!title field can not contain any HTML tags exept !allowed',
'placeholders' => array('!title' => $title, '!allowed' => $allowed_tags),
'error_type' => 'plaintext',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'plaintext',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['plaintext'] = theme('clientside_error', $variables);
}
/**
* Set validation rule for required fields that must equal a value from an other field.
*/
......@@ -644,6 +741,31 @@ function _clientside_validation_set_specific_values($name, $title, $values, &$js
$js_rules[$name]['messages']['specificVals'] = $message;
}
/**
* Set validation rule for fields that can not consist of one or more specific values
*/
function _clientside_validation_set_blacklist($name, $title, $values, &$js_rules, $message='') {
$js_rules[$name]['blacklist'] = $values;
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
if (empty($message)) {
$variables = array(
'message' => '!title field can not consist of following elements: !elements.',
'placeholders' => array('!title' => $title, '!elements' => implode(', ', $values)),
'error_type' => 'blacklist',
'element_name' => $name
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'blacklist',
'element_name' => $name
);
}
$message = theme('clientside_error', $variables);
$js_rules[$name]['messages']['blacklist'] = $message;
}
/**
* Set validation rule for ean number fields.
*/
......
......@@ -44,7 +44,78 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : t('Invalid value for !name', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_regex($el_name, $el_title, $js_rules, $rule['data'], $message);
break;
case 'min_length':
$message = t('!name field has a minimum length of !minl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!minl' => $rule['data']));
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['data'], '', $js_rules, $message);
break;
case 'max_length':
$message = isset($rule['error_message']) ? $rule['error_message'] : t('!name field has a maximum length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!maxl' => $rule['data']));
_clientside_validation_set_minmaxlength($el_name, $el_title, '', $rule['data'], $js_rules, $message);
break;
case 'min_words':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmax_words($el_name, $el_title, $rule['data'], '', $js_rules, $message);
break;
case 'max_words':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmax_words($el_name, $el_title, '', $rule['data'], $js_rules, $message);
break;
case 'plain_text':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_plain_text($el_name, $el_title, $rule['data'], $js_rules, $message);
break;
case 'must_be_empty':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_specific_value($el_name, $el_title, '', $js_rules, $message);
break;
case 'blacklist':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
$blacklist = explode(',', $rule['data']);
$blacklist = array_map('trim', $blacklist);
_clientside_validation_set_blacklist($el_name, $el_title, $blacklist, $js_rules, $message);
break;
case 'numeric':
$data = $rule['data'];
$range = array('min' => NULL, 'max' => NULL);
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
if (strpos($data, '|') !== FALSE) {
list($min, $max) = explode('|', $data);
if ($min != '' && is_numeric($min)) {
$range['min'] = (int) $min;
}
if ($max != '' && is_numeric($max)) {
$range['max'] = (int) $max;
}
}
else {
if ($data != '' && is_numeric($data)) {
$range['min'] = (int) $data;
}
}
if (!empty($range['min']) || !empty($range['max'])) {
_clientside_validation_set_minmax($el_name, $el_title, $range['min'], $range['max'], $js_rules, $message);
}
else {
_clientside_validation_set_number($el_name, $el_title, $js_rules, $message);
}
break;
case 'specific_value':
$value = explode(',', $rule['data']);
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_specific_value($el_name, $el_title, $value, $js_rules, $message);
break;
case 'select_min':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['data'], '', $js_rules, $message);
break;
case 'select_max':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmaxlength($el_name, $el_title, '', $rule['data'], $js_rules, $message);
break;
case 'select_exact':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['data'], $rule['data'], $js_rules, $message);
break;
default:
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : t('Invalid value for !name', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', '')));
$context = array('type' => 'field_validation', 'rule' => $rule, 'message' => $message);
......
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