Commit 9fe03f52 authored by Jelle's avatar Jelle
Browse files

Issue #2051667 by Denis Danielyan: Added Webform 7.x-4.0-alpha9 'negate flag' does not work.

parent bcdf9db4
......@@ -815,13 +815,13 @@
jQuery.validator.addMethod("oneOf", function(value, element, param) {
for (var p in param.values) {
if (param.values[p] === value && param.caseSensitive) {
return true;
return !param.negate;
}
else if (param.values[p].toLowerCase() === value.toLowerCase() && !param.caseSensitive) {
return true;
return !param.negate;
}
}
return false;
return param.negate;
}, jQuery.format(''));
jQuery.validator.addMethod("specificVals", function(value, element, param){
......@@ -838,11 +838,11 @@
value = value.split(' ');
}
for (var i in value) {
if(param.indexOf(value[i]) !== -1) {
return false;
if(param.values.indexOf(value[i]) !== -1) {
return param.negate;
}
}
return true;
return !param.negate;
});
// Default regular expression support
......@@ -936,11 +936,11 @@
return this.optional(element);
}
else {
var regexp = new RegExp(param[0], param[1]);
var regexp = new RegExp(param.regex[0], param.regex[1]);
if(regexp.test(value)){
return true;
return !param.negate;
}
return false;
return param.negate;
}
}, jQuery.format('The value does not match the expected format.'));
......@@ -984,19 +984,32 @@
}, jQuery.format('The value must be fewer than {0} words long'));
jQuery.validator.addMethod("plaintext", function(value, element, param){
return this.optional(element) || (value === strip_tags(value, param));
var result = param.negate ? (value !== strip_tags(value, param.tags)) : (value === strip_tags(value, param.tags));
return this.optional(element) || result;
}, jQuery.format('The value must be plaintext'));
jQuery.validator.addMethod("selectMinlength", function(value, element, param) {
return this.optional(element) || $(element).find('option:selected').length >= param;
var result = $(element).find('option:selected').length >= param.min;
if (param.negate) {
result = !result;
}
return this.optional(element) || result;
}, jQuery.format('You must select at least {0} values'));
jQuery.validator.addMethod("selectMaxlength", function(value, element, param) {
return this.optional(element) || $(element).find('option:selected').length <= param;
var result = $(element).find('option:selected').length <= param.max;
if (param.negate) {
result = !result;
}
return this.optional(element) || result;
}, jQuery.format('You must select a maximum of {0} values'));
jQuery.validator.addMethod("selectRangelength", function(value, element, param) {
return this.optional(element) || ($(element).find('option:selected').length >= param[0] && $(element).find('option:selected').length <= param[1]);
var result = ($(element).find('option:selected').length >= param.range[0] && $(element).find('option:selected').length <= param.range[1]);
if (param.negate) {
result = !result;
}
return this.optional(element) || result;
}, jQuery.format('You must select at between {0} and {1} values'));
jQuery.validator.addMethod("datemin", function(value, element, param) {
......
......@@ -763,13 +763,14 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
/**
* Set validation rule for fields with a minimum and/or maximum length for select options.
*/
function _clientside_validation_set_minmaxlength_select($name, $title, $min, $max, &$js_rules, $message = '') {
function _clientside_validation_set_minmaxlength_select($name, $title, $min, $max, &$js_rules, $message = '', $negate = FALSE) {
$title = _clientside_validation_set_title($title);
if ($max == 1) {
$max = '';
}
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['selectRangelength'] = array($min, $max);
$js_rules[$name]['selectRangelength']['range'] = array($min, $max);
$js_rules[$name]['selectRangelength']['negate'] = $negate;
if (empty($message)) {
$variables = array(
'message' => '!title field has to have between !min and !max values.',
......@@ -788,7 +789,7 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
$js_rules[$name]['messages']['selectRangelength'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$js_rules[$name]['selectMinlength'] = $min;
$js_rules[$name]['selectMinlength'] = array('min' => $min, 'negate' => $negate);
if (empty($message)) {
$variables = array(
'message' => '!title field has to have minimal !min values.',
......@@ -807,7 +808,7 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
$js_rules[$name]['messages']['selectMinlength'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$js_rules[$name]['selectMaxlength'] = $max;
$js_rules[$name]['selectMaxlength'] = array('max' => $max, 'negate' => $negate);
if (empty($message)) {
$variables = array(
'message' => '!title field has to have maximum !max values.',
......@@ -894,9 +895,10 @@ function _clientside_validation_set_minmax_words($name, $title, $min, $max, &$js
/**
* Set validation rule for plain text fields
*/
function _clientside_validation_set_plain_text($name, $title, $allowed_tags, &$js_rules, $message = '') {
function _clientside_validation_set_plain_text($name, $title, $allowed_tags, &$js_rules, $message = '', $negate = FALSE) {
$title = _clientside_validation_set_title($title);
$js_rules[$name]['plaintext'] = empty($allowed_tags) ? '' : $allowed_tags;
$js_rules[$name]['plaintext']['tags'] = empty($allowed_tags) ? '' : $allowed_tags;
$js_rules[$name]['plaintext']['negate'] = $negate;
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',
......@@ -981,11 +983,11 @@ function _clientside_validation_set_not_equal($name, $title, $elements, &$js_rul
/**
* Set validation rule for fields that must be equal to a specific value.
*/
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '', $case_sensitive = TRUE) {
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '', $case_sensitive = TRUE, $negate = FALSE) {
if (!is_array($value)) {
$value = array($value);
}
$js_rules[$name]['oneOf'] = array('values' => $value, 'caseSensitive' => $case_sensitive);
$js_rules[$name]['oneOf'] = array('values' => $value, 'caseSensitive' => $case_sensitive, 'negate' => $negate);
$title = _clientside_validation_set_title($title);
if (empty($message)) {
$variables = array(
......@@ -1034,8 +1036,9 @@ function _clientside_validation_set_specific_values($name, $title, $values, &$js
/**
* 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;
function _clientside_validation_set_blacklist($name, $title, $values, &$js_rules, $message = '', $negate = FALSE) {
$js_rules[$name]['blacklist']['values'] = $values;
$js_rules[$name]['blacklist']['negate'] = $negate;
$title = _clientside_validation_set_title($title);
if (empty($message)) {
$variables = array(
......@@ -1084,7 +1087,7 @@ function _clientside_validation_set_ean($name, $title, &$js_rules, $message = ''
/**
* Set validation rule for fields with regex validation from webform_validation
*/
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "", $type = 'regex') {
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "", $type = 'regex', $negate = FALSE) {
$title = _clientside_validation_set_title($title);
if (empty($message)) {
$variables = array(
......@@ -1102,9 +1105,10 @@ function _clientside_validation_set_regex($name, $title, &$js_rules, $expression
);
}
$message = theme('clientside_error', $variables);
$js_rules[$name]['regexMatch'] = array($expression);
$js_rules[$name]['regexMatch']['regex'] = array($expression);
$js_rules[$name]['regexMatch']['negate'] = $negate;
if (!empty($modifiers)) {
$js_rules[$name]['regexMatch'][] = $modifiers;
$js_rules[$name]['regexMatch']['regex'][] = $modifiers;
}
$js_rules[$name]['messages']['regexMatch'] = $message;
}
......
......@@ -141,6 +141,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
if ($webform_validation_rules = _clientside_validation_webform_webform_validation($form_state['values']['details']['nid'], $form)) {
$checkboxrules = array();
foreach ($webform_validation_rules as $webform_validation_rule) {
$negate = isset($webform_validation_rule['negate']) && $webform_validation_rule['negate'];
switch ($webform_validation_rule['validator']) {
case 'min_length':
foreach ($webform_validation_rule['components'] as $component) {
......@@ -157,13 +158,13 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
case 'min_words':
foreach ($webform_validation_rule['components'] as $component) {
$message = t('!name field must have at least !min words.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['element_title'] . variable_get('clientside_validation_suffix', ''), '!min' => $webform_validation_rule['data']));
_clientside_validation_set_minmax_words($component['element_name'], $component['element_title'], $webform_validation_rule['data'], "", $js_rules);
_clientside_validation_set_minmax_words($component['element_name'], $component['element_title'], $webform_validation_rule['data'], "", $js_rules, $message);
}
break;
case 'max_words':
foreach ($webform_validation_rule['components'] as $component) {
$message = t('!name field can not have more than !max words.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['element_title'] . variable_get('clientside_validation_suffix', ''), '!max' => $webform_validation_rule['data']));
_clientside_validation_set_minmax_words($component['element_name'], $component['element_title'], "", $webform_validation_rule['data'], $js_rules);
_clientside_validation_set_minmax_words($component['element_name'], $component['element_title'], "", $webform_validation_rule['data'], $js_rules, $message);
}
break;
case 'numeric':
......@@ -204,7 +205,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
break;
case 'plain_text':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_plain_text($component['element_name'], $component['element_title'], "", $js_rules);
_clientside_validation_set_plain_text($component['element_name'], $component['element_title'], "", $js_rules, '', $negate);
}
break;
case 'blacklist':
......@@ -212,7 +213,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
$blacklist = explode(',', $webform_validation_rule['data']);
$blacklist = array_map('trim', $blacklist);
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_blacklist($component['element_name'], $component['element_title'], $blacklist, $js_rules, $message);
_clientside_validation_set_blacklist($component['element_name'], $component['element_title'], $blacklist, $js_rules, $message, $negate);
}
break;
case 'equal':
......@@ -235,7 +236,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
foreach ($webform_validation_rule['components'] as $component) {
$value = explode(',', $webform_validation_rule['data']);
$message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : '';
_clientside_validation_set_specific_value($component['element_name'], $component['element_title'], $value, $js_rules, $message);
_clientside_validation_set_specific_value($component['element_name'], $component['element_title'], $value, $js_rules, $message, TRUE, $negate);
}
break;
case 'select_min':
......@@ -307,7 +308,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
if (isset($component['extra']['multiple']) && $component['extra']['multiple']) {
$el_name .= '[]';
}
_clientside_validation_set_minmaxlength_select($el_name, $component['element_title'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules);
_clientside_validation_set_minmaxlength_select($el_name, $component['element_title'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules, '', $negate);
}
}
break;
......@@ -321,7 +322,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
foreach ($webform_validation_rule['components'] as $component) {
$message = $webform_validation_rule['error_message'];
$expression = $webform_validation_rule['data'];
_clientside_validation_set_regex($component['element_name'], $component['element_title'], $js_rules, $expression, $message);
_clientside_validation_set_regex($component['element_name'], $component['element_title'], $js_rules, $expression, $message, '', 'regex', $negate);
}
break;
case 'must_be_empty':
......@@ -333,7 +334,7 @@ function clientside_validation_webform_add_webform_validation($form_id, &$form,
default:
foreach ($webform_validation_rule['components'] as $component) {
$message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : t('Invalid value for !name', array('!name' => variable_get('clientside_validation_prefix', '') . $component['element_title'] . variable_get('clientside_validation_suffix', '')));
$context = array('type' => 'webform', 'rule' => $webform_validation_rule, 'message' => $message);
$context = array('type' => 'webform', 'rule' => $webform_validation_rule, 'message' => $message, 'negate' => $negate);
drupal_alter('clientside_validation_rule', $js_rules, $component, $context);
}
break;
......
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