Commit 2a7263d6 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1270996 by mjpa: Added Control placement of error container.

parent 206d5fc2
......@@ -131,6 +131,91 @@ function clientside_validation_admin_settings() {
$form['clientside_validation_error']['clientside_validation_scroll_speed'] += array('#rules' => array('numeric'));
}
//Error Placement
$form['clientside_validation_error_placement'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Error message placement'),
'#description' => t('Change error message placement settings.'),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_default'] = array(
'#type' => 'select',
'#title' => t('Default location'),
'#description' => t('Default location to show the error messages.'),
'#options' => array(
CLIENTSIDE_VALIDATION_JQUERY_SELECTOR => t('jQuery selector'),
CLIENTSIDE_VALIDATION_TOP_OF_FORM => t('Top of form'),
CLIENTSIDE_VALIDATION_BEFORE_LABEL => t('Before label'),
CLIENTSIDE_VALIDATION_AFTER_LABEL => t('After label'),
CLIENTSIDE_VALIDATION_BEFORE_INPUT => t('Before input'),
CLIENTSIDE_VALIDATION_AFTER_INPUT => t('After input'),
CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM => t('Top of first form'),
),
'#default_value' => variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM),
);
$form['clientside_validation_error_placement']['clientside_validation_jquery_selector'] = array(
'#type' => 'textfield',
'#title' => t('jQuery selector'),
'#description' => t('Enter a jQuery selector here if you selected "jQuery selector" in the previous step.
The error messages will be shown in this div if it exists.
If it doesn\'t, error messages will be shown above the first form on the page.'),
'#default_value' => variable_get('clientside_validation_jquery_selector', ''),
);
//Error placement exceptions
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Error message placement exceptions'),
'#description' => t('Add exceptions to the default error message placement.'),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_jquery'] = array(
'#type' => 'textarea',
'#title' => t('jQuery selector forms'),
'#description' => t('Enter form IDs of the forms of which the errors should appear in the div with the entered jQuery selector.'),
'#default_value' => variable_get('clientside_validation_error_placement_jquery', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_top_of_form'] = array(
'#type' => 'textarea',
'#title' => t('Top of form'),
'#description' => t('Enter form IDs of the forms of which the errors should appear right above the form.'),
'#default_value' => variable_get('clientside_validation_error_placement_top_of_form', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_before_label'] = array(
'#type' => 'textarea',
'#title' => t('Before label'),
'#description' => t('Enter form IDs of the forms of which the errors should appear before the label of the element.'),
'#default_value' => variable_get('clientside_validation_error_placement_before_label', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_after_label'] = array(
'#type' => 'textarea',
'#title' => t('Before label'),
'#description' => t('Enter form IDs of the forms of which the errors should appear after the label of the element.'),
'#default_value' => variable_get('clientside_validation_error_placement_after_label', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_before_input'] = array(
'#type' => 'textarea',
'#title' => t('Before input'),
'#description' => t('Enter form IDs of the forms of which the errors should appear before the input element.'),
'#default_value' => variable_get('clientside_validation_error_placement_before_input', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_after_input'] = array(
'#type' => 'textarea',
'#title' => t('After input'),
'#description' => t('Enter form IDs of the forms of which the errors should appear after the input element.'),
'#default_value' => variable_get('clientside_validation_error_placement_after_input', ''),
);
//Page settings
$form['clientside_validation_pages'] = array(
'#type' => 'fieldset',
......
......@@ -49,21 +49,8 @@
var self = this;
jQuery.each (self.forms, function(f) {
self.groups[f] = {};
// Add error container above the form, first look for standard message container
var errorel = self.prefix + f + '-errors';
if ($('div.messages.error').length) {
if ($('div.messages.error').attr('id').length) {
errorel = $('div.messages.error').attr('id');
}
else {
$('div.messages.error').attr('id', errorel);
}
}
else if (!$('#' + errorel).length) {
$('<div id="' + errorel + '" class="messages error clientside-error"><ul></ul></div>').insertBefore('#' + f).hide();
}
self.groups[f] = {};
// Remove any existing validation stuff
if (self.validators[f]) {
// Doesn't work :: $('#' + f).rules('remove');
......@@ -97,9 +84,6 @@
if(typeof $('#' + f).validate == 'function') {
var validate_options = {
errorClass: 'error',
errorContainer: '#' + errorel,
errorLabelContainer: '#' + errorel + ' ul',
wrapper: 'li',
groups: self.groups[f],
unhighlight: function(element, errorClass, validClass) {
// Default behavior
......@@ -146,13 +130,109 @@
}
}
if (self.data.general.scrollTo) {
$("#" + errorel).show();
var x = $("#" + errorel).offset().top - $("#" + errorel).height() - 100; // provides buffer in viewport
if ($("#" + errorel).length) {
$("#" + errorel).show();
var x = $("#" + errorel).offset().top - $("#" + errorel).height() - 100; // provides buffer in viewport
}
else {
var x = $(validator.errorList[0].element).offset().top - $(validator.errorList[0].element).height() - 100;
}
$('html, body').animate({scrollTop: x}, self.data.general.scrollSpeed);
}
}
}
};
//CLIENTSIDE_VALIDATION_JQUERY_SELECTOR: 0
//CLIENTSIDE_VALIDATION_TOP_OF_FORM: 1
//CLIENTSIDE_VALIDATION_BEFORE_LABEL: 2
//CLIENTSIDE_VALIDATION_AFTER_LABEL: 3
//CLIENTSIDE_VALIDATION_BEFORE_INPUT: 4
//CLIENTSIDE_VALIDATION_AFTER_INPUT: 5
//CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM: 6
switch (parseInt(self.forms[f].errorPlacement)) {
case 0:
if ($(self.forms[f].errorJquerySelector).length) {
if (!$(self.forms[f].errorJquerySelector + ' #' + errorel).length) {
$('<div id="' + errorel + '" class="messages error clientside-error"><ul></ul></div>').prependTo(self.forms[f].errorJquerySelector).hide();
}
}
else if (!$('#' + errorel).length) {
$('<div id="' + errorel + '" class="messages error clientside-error"><ul></ul></div>').insertBefore('#' + f).hide();
}
validate_options.errorContainer = '#' + errorel;
validate_options.errorLabelContainer = '#' + errorel + ' ul';
validate_options.wrapper = 'li';
break;
case 1:
if (!$('#' + errorel).length) {
$('<div id="' + errorel + '" class="messages error clientside-error"><ul></ul></div>').insertBefore('#' + f).hide();
}
validate_options.errorContainer = '#' + errorel;
validate_options.errorLabelContainer = '#' + errorel + ' ul';
validate_options.wrapper = 'li';
break;
case 2:
validate_options.errorPlacement = function(error, element) {
if (element.is(":radio")) {
error.insertBefore(element.parents('.form-radios').prev('label'));
}
else if (element.is(":checkbox")) {
error.insertBefore(element.parents('.form-checkboxes').prev('label'));
}
else {
error.insertBefore('label[for="'+ element.attr('id') +'"]');
}
}
break;
case 3:
validate_options.errorPlacement = function(error, element) {
if (element.is(":radio")) {
error.insertAfter(element.parents('.form-radios').prev('label'));
}
else if (element.is(":checkbox")) {
error.insertAfter(element.parents('.form-checkboxes').prev('label'));
}
else {
error.insertAfter('label[for="'+ element.attr('id') +'"]');
}
}
break;
case 4:
validate_options.errorPlacement = function(error, element) {
error.insertBefore(element);
}
break;
case 5:
validate_options.errorPlacement = function(error, element) {
if (element.is(":radio")) {
error.insertAfter(element.parents('.form-radios'));
}
else if (element.is(":checkbox")) {
error.insertAfter(element.parents('.form-checkboxes'));
}
else {
error.insertAfter(element);
}
}
break;
case 6:
if ($('div.messages.error').length) {
if ($('div.messages.error').attr('id').length) {
errorel = $('div.messages.error').attr('id');
}
else {
$('div.messages.error').attr('id', errorel);
}
}
else if (!$('#' + errorel).length) {
$('<div id="' + errorel + '" class="messages error clientside-error"><ul></ul></div>').insertBefore('#' + f).hide();
}
validate_options.errorContainer = '#' + errorel;
validate_options.errorLabelContainer = '#' + errorel + ' ul';
validate_options.wrapper = 'li';
break;
}
if (!self.forms[f].includeHidden) {
validate_options.ignore = ':input:hidden';
......
......@@ -10,6 +10,15 @@ define('CLIENTSIDE_VALIDATION_VALIDATE_ALL_EXCEPT', 2);
define('CLIENTSIDE_VALIDATION_EXCLUDE_PATHS', 0); //Add validation on all paths except those specified
define('CLIENTSIDE_VALIDATION_INCLUDE_PATHS', 1); //Only add validation on specified paths
define('CLIENTSIDE_VALIDATION_JQUERY_SELECTOR', 0);
define('CLIENTSIDE_VALIDATION_TOP_OF_FORM', 1);
define('CLIENTSIDE_VALIDATION_BEFORE_LABEL', 2);
define('CLIENTSIDE_VALIDATION_AFTER_LABEL', 3);
define('CLIENTSIDE_VALIDATION_BEFORE_INPUT', 4);
define('CLIENTSIDE_VALIDATION_AFTER_INPUT', 5);
define('CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM', 6);
/**
* Implements hook_menu().
*/
......@@ -110,14 +119,6 @@ function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
break;
}
}
// Add a setting for this form if the hidden elements should still be validated
$include_hidden_setting = variable_get('clientside_validation_include_hidden', '');
$include_hidden = preg_split('/(\r\n?|\n)/', $include_hidden_setting, -1, PREG_SPLIT_NO_EMPTY);
if (in_array($form_id, $include_hidden)) {
$setting['clientsideValidation']['forms'][$form['#id']]['includeHidden'] = TRUE;
drupal_add_js($setting, 'setting');
}
}
/**
......@@ -128,11 +129,7 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
drupal_alter("clientside_validation_webform", $form, $form_state, $js_rules);
if (!empty($js_rules)) {
$settings = array();
_clientside_validation_add_general_settings($settings);
$settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
"errorContainer" => "#formerrors-" . $form['#id'],
"errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
);
_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(
......@@ -176,11 +173,7 @@ function clientside_validation_form_after_build(&$form, &$form_state) {
drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);
if (!empty($js_rules)) {
$settings = array();
_clientside_validation_add_general_settings($settings);
$settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
"errorContainer" => "#formerrors-" . $form['#id'],
"errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
);
_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(
......@@ -212,7 +205,52 @@ function clientside_validation_form_after_build(&$form, &$form_state) {
return $form;
}
function _clientside_validation_add_general_settings(&$settings) {
function _clientside_validation_add_general_settings(&$settings, $form_id) {
// Add a setting for this form if the hidden elements should still be validated
$include_hidden_setting = variable_get('clientside_validation_include_hidden', '');
$include_hidden = preg_split('/(\r\n?|\n)/', $include_hidden_setting, -1, PREG_SPLIT_NO_EMPTY);
if (in_array(str_replace('-', '_', $form_id), $include_hidden)) {
$setting['clientsideValidation']['forms'][$form_id]['includeHidden'] = TRUE;
}
$jquery = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_jquery', ''), -1, PREG_SPLIT_NO_EMPTY);
$top_of_form = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_top_of_form', ''), -1, PREG_SPLIT_NO_EMPTY);
$before_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_label', ''), -1, PREG_SPLIT_NO_EMPTY);
$after_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_label', ''), -1, PREG_SPLIT_NO_EMPTY);
$before_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_input', ''), -1, PREG_SPLIT_NO_EMPTY);
$after_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_input', ''), -1, PREG_SPLIT_NO_EMPTY);
$jquery_selector = variable_get('clientside_validation_jquery_selector', '');
if (in_array(str_replace('-', '_', $form_id), $jquery) && !empty($jquery_selector)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_JQUERY_SELECTOR;
$settings['clientsideValidation']['forms'][$form_id]['errorJquerySelector'] = $jquery_selector;
}
elseif (in_array(str_replace('-', '_', $form_id), $top_of_form)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FORM;
}
elseif (in_array(str_replace('-', '_', $form_id), $before_label)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_BEFORE_LABEL;
}
elseif (in_array(str_replace('-', '_', $form_id), $after_label)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_AFTER_LABEL;
}
elseif (in_array(str_replace('-', '_', $form_id), $before_input)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_BEFORE_INPUT;
}
elseif (in_array(str_replace('-', '_', $form_id), $after_input)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_AFTER_INPUT;
}
else {
if (variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM) == CLIENTSIDE_VALIDATION_JQUERY_SELECTOR
&& empty($jquery_selector)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM;
}
else {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM);
$settings['clientsideValidation']['forms'][$form_id]['errorJquerySelector'] = $jquery_selector;
}
}
$form_settings_added[] = str_replace('-', '_', $form_id);
$settings['clientsideValidation']['general'] = array(
"errorClass" => "error",
"wrapper" => "li",
......
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