Commit 1d830e0a authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1305512 by opteronmx: Fixed Notice: Undefined index: #year_start().

parent 2b3de924
......@@ -30,8 +30,12 @@
this.groups = {};
//disable class and attribute rules
$.validator.classRules = function() { return {}; };
$.validator.attributeRules = function() { return {}; };
$.validator.classRules = function() {
return {};
};
$.validator.attributeRules = function() {
return {};
};
this.addExtraRules();
this.bindForms();
......@@ -86,6 +90,24 @@
});
}
if('daterangerules' in self.forms[f]){
groupkey = "";
jQuery.each (self.forms[f]['daterangerules'], function(r) {
groupkey = r + '_group';
self.groups[f][groupkey] = "";
jQuery.each(this, function(){
i = 0;
$('#' + f + ' #' + r + ' :input').not('input[type=image]').each(function(){
if(i > 0){
self.groups[f][groupkey] += ' ';
}
self.groups[f][groupkey] += $(this).attr('name');
i++;
});
});
});
}
// Add basic settings
//@todo: find cleaner fix
// ugly fix for nodes in colorbox
......@@ -285,6 +307,23 @@
Drupal.clientsideValidation.prototype.bindRules = function(formid){
var self = this;
var hideErrordiv = function(){
//wait just one milisecond until the error div is updated
window.setTimeout(function(){
var visibles = 0;
$("div.messages.error ul li").each(function(){
if($(this).is(':visible')){
visibles++;
}
else {
$(this).remove();
}
});
if(visibles < 1){
$("div.messages.error").hide();
}
}, 1);
};
if('checkboxrules' in self.forms[formid]){
jQuery.each (self.forms[formid]['checkboxrules'], function(r) {
$("#" + formid + " " + this['checkboxgroupminmax'][2] + ' :input[type="checkbox"]').addClass('require-one');
......@@ -294,27 +333,19 @@
if ($("#" + formid + " " + this['checkboxgroupminmax'][2] + " .require-one").length) {
$("#" + formid + " " + this['checkboxgroupminmax'][2] + " .require-one").each(function(){
$(this).rules("add", self.forms[formid]['checkboxrules'][r]);
$(this).change(function(){
//wait just one milisecond until the error div is updated
window.setTimeout(function(){
var visibles = 0;
$("div.messages.error ul li").each(function(){
if($(this).is(':visible')){
visibles++;
}
else {
$(this).remove();
}
});
if(visibles < 1){
$("div.messages.error").hide();
}
}, 1);
});
$(this).change(hideErrordiv);
});
}
});
}
if('daterangerules' in self.forms[formid]){
jQuery.each (self.forms[formid]['daterangerules'], function(r) {
$('#' + formid + ' #' + r + ' :input').not('input[type=image]').each(function(){
$(this).rules("add", self.forms[formid]['daterangerules'][r]);
$(this).blur(hideErrordiv);
});
});
}
if('rules' in self.forms[formid]){
jQuery.each (self.forms[formid]['rules'], function(r) {
// Check if element exist in DOM before adding the rule
......@@ -437,6 +468,63 @@
}, 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]']");
}
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;
}
}
}
if (parseInt(yearelem.val(), 10) > parseInt(param[1][0], 10)) {
return false;
}
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;
}
}
}
yearelem.removeClass('error');
monthelem.removeClass('error');
dayelem.removeClass('error');
return true;
}, jQuery.format('The value does not match the expected format.'));
// EAN code
jQuery.validator.addMethod("validEAN", function(value, element, param) {
if (this.optional(element) && value == '') {
......
......@@ -131,22 +131,8 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
if (!empty($js_rules)) {
$settings = array();
_clientside_validation_add_general_settings($settings, $form['#id']);
foreach ($js_rules as $key => $rule) {
if (isset($rule['checkboxgroupminmax'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
);
unset($rule['checkboxgroupminmax']);
unset($rule['messages']['checkboxgroupminmax']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (!empty($rule)) {
$settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
}
}
_clientside_validation_add_special_rules($js_rules, $settings, $form);
if (variable_get('clientside_validation_use_minified', 0) == 0) {
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
......@@ -161,6 +147,58 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
return $form;
}
function _clientside_validation_add_special_rules(&$js_rules, &$settings, $form) {
foreach ($js_rules as $key => $rule) {
if (isset($rule['checkboxgroupminmax'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
);
unset($rule['checkboxgroupminmax']);
unset($rule['messages']['checkboxgroupminmax']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (isset($rule['daterange'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['daterangerules'][$key] = array(
'daterange' => $rule['daterange'],
'messages' => array('daterange' => $rule['messages']['daterange'])
);
unset($rule['daterange']);
unset($rule['messages']['daterange']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (isset($rule['datemin'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['dateminrules'][$key] = array(
'daterange' => $rule['datemin'],
'messages' => array('datemin' => $rule['messages']['datemin'])
);
unset($rule['datemin']);
unset($rule['messages']['datemin']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (isset($rule['datemax'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['datemaxrules'][$key] = array(
'daterange' => $rule['datemax'],
'messages' => array('datemax' => $rule['messages']['datemax'])
);
unset($rule['datemax']);
unset($rule['messages']['datemax']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (!empty($rule)) {
$settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
}
}
}
/**
* Regular form.
*/
......@@ -175,22 +213,7 @@ function clientside_validation_form_after_build(&$form, &$form_state) {
if (!empty($js_rules)) {
$settings = array();
_clientside_validation_add_general_settings($settings, $form['#id']);
foreach ($js_rules as $key => $rule) {
if (isset($rule['checkboxgroupminmax'])) {
$settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
);
unset($rule['checkboxgroupminmax']);
unset($rule['messages']['checkboxgroupminmax']);
if (empty($rule['messages'])) {
unset($rule['messages']);
}
}
if (!empty($rule)) {
$settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
}
}
_clientside_validation_add_special_rules($js_rules, $settings, $form);
if (variable_get('clientside_validation_use_minified', 0) == 0) {
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
}
......@@ -403,6 +426,67 @@ function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules
}
}
function _clientside_validation_set_minmax_date($name, $title, $start_date, $end_date, &$js_rules, $message = '') {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
if (isset($start_date) && $start_date !== '' && isset($end_date) && $end_date !== '') {
$js_rules[$name]['daterange'] = array($start_date, $end_date);
if (empty($message)) {
$variables = array(
'message' => '!title field has to be between !min and !max.',
'placeholders' => array('!title' => $title, '!min' => $start_date, '!max' => $end_date),
'error_type' => 'daterange',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'daterange',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['daterange'] = theme('clientside_error', $variables);
}
elseif (isset($start_date) && $start_date !== '') {
$js_rules[$name]['datemin'] = $start_date;
if (empty($message)) {
$variables = array(
'message' => '!title field has to be greater than !min.',
'placeholders' => array('!title' => $title, '!min' => $start_date),
'error_type' => 'datemin',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'datemin',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['datemin'] = theme('clientside_error', $variables);
}
elseif (isset($end_date) && $end_date !== '') {
$js_rules[$name]['datemax'] = $end_date;
if (empty($message)) {
$variables = array(
'message' => '!title field has to be smaller than !max.',
'placeholders' => array('!title' => $title, '!max' => $end_date),
'error_type' => 'datemax',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'datemax',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['datemax'] = theme('clientside_error', $variables);
}
}
/**
* Set validation rule for fields with a minimum and/or maximum length.
*/
......
......@@ -37,9 +37,17 @@ function clientside_validation_webform_after_build_recurse($form_id, &$form, &$f
_clientside_validation_set_required($element['#name'] . '[day]', $element['#title'], TRUE, $js_rules, $message);
$message = t('Year in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['#name'] . '[year]', $element['#title'], TRUE, $js_rules, $message);
if (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
$message = t('The entered date needs to be between the years @start and @end.', array('@start' => $element['#year_start'], '@end' => $element['#year_end']));
_clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
if (isset($element['#year_start']) && isset($element['#year_end'])) {
if (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
$message = t('The entered date needs to be between the years @start and @end.', array('@start' => $element['#year_start'], '@end' => $element['#year_end']));
_clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
}
}
elseif (isset($element['#start_date']) && isset($element['#end_date'])) {
$message = t('The entered date needs to be between the dates @start and @end.', array('@start' => $element['#start_date'], '@end' => $element['#end_date']));
$start_date = explode('-', $element['#start_date']);
$end_date = explode('-', $element['#end_date']);
_clientside_validation_set_minmax_date('webform-component-' . $element['#webform_component']['name'], $element['#title'], $start_date, $end_date, $js_rules, $message);
}
}
elseif ($element['#type'] == 'checkboxes') {
......
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