Commit 0dd12488 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

allow custom javascript function for error placement

parent 2a7263d6
......@@ -143,7 +143,15 @@ function clientside_validation_admin_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.'),
'#description' => t('Default location to show the error messages.
If you choose "jQuery selector", provide a valid jQuery selector in the appropriate textfield.
If you choose "Custom function", provide the name of the custom javascript function.
This function will be given to !errorPlacement',
array(
'!errorPlacement' =>
l('the errorPlacement option of jQuery.validate()', 'http://docs.jquery.com/Plugins/Validation/validate#options')
)
),
'#options' => array(
CLIENTSIDE_VALIDATION_JQUERY_SELECTOR => t('jQuery selector'),
CLIENTSIDE_VALIDATION_TOP_OF_FORM => t('Top of form'),
......@@ -152,6 +160,7 @@ function clientside_validation_admin_settings() {
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'),
CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION => t('Custom function'),
),
'#default_value' => variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM),
);
......@@ -165,6 +174,18 @@ function clientside_validation_admin_settings() {
'#default_value' => variable_get('clientside_validation_jquery_selector', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_custom_error_function'] = array(
'#type' => 'textfield',
'#title' => t('Custom function name'),
'#description' => t('If you selected "Custom function" in the previous step, provide the name of the custom javascript function.
This function will be given to !errorPlacement',
array(
'!errorPlacement' =>
l('the errorPlacement option of jQuery.validate()', 'http://docs.jquery.com/Plugins/Validation/validate#options')
)),
'#default_value' => variable_get('clientside_validation_custom_error_function', ''),
);
//Error placement exceptions
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions'] = array(
'#type' => 'fieldset',
......@@ -216,6 +237,13 @@ function clientside_validation_admin_settings() {
'#default_value' => variable_get('clientside_validation_error_placement_after_input', ''),
);
$form['clientside_validation_error_placement']['clientside_validation_error_placement_exceptions']['clientside_validation_error_placement_custom_function'] = array(
'#type' => 'textarea',
'#title' => t('Custom function'),
'#description' => t('Enter form IDs of the forms of which the errors should appear according to the the custom function.'),
'#default_value' => variable_get('clientside_validation_error_placement_custom_function', ''),
);
//Page settings
$form['clientside_validation_pages'] = array(
'#type' => 'fieldset',
......
......@@ -150,6 +150,7 @@
//CLIENTSIDE_VALIDATION_BEFORE_INPUT: 4
//CLIENTSIDE_VALIDATION_AFTER_INPUT: 5
//CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM: 6
//CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION: 7
switch (parseInt(self.forms[f].errorPlacement)) {
case 0:
if ($(self.forms[f].errorJquerySelector).length) {
......@@ -232,6 +233,11 @@
validate_options.errorLabelContainer = '#' + errorel + ' ul';
validate_options.wrapper = 'li';
break;
case 7:
validate_options.errorPlacement = function (error, element) {
window[self.forms[f].customErrorFunction](error, element);
}
break;
}
if (!self.forms[f].includeHidden) {
......
......@@ -18,6 +18,7 @@ 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);
define('CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION', 7);
/**
* Implements hook_menu().
......@@ -218,6 +219,7 @@ function _clientside_validation_add_general_settings(&$settings, $form_id) {
$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);
$custom_function = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_custom_function', ''), -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', '');
......@@ -240,6 +242,10 @@ function _clientside_validation_add_general_settings(&$settings, $form_id) {
elseif (in_array(str_replace('-', '_', $form_id), $after_input)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_AFTER_INPUT;
}
elseif (in_array(str_replace('-', '_', $form_id), $custom_function)) {
$settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION;
$settings['clientsideValidation']['forms'][$form_id]['customErrorFunction'] = variable_get('clientside_validation_custom_error_function', '');
}
else {
if (variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM) == CLIENTSIDE_VALIDATION_JQUERY_SELECTOR
&& empty($jquery_selector)) {
......@@ -248,6 +254,7 @@ function _clientside_validation_add_general_settings(&$settings, $form_id) {
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;
$settings['clientsideValidation']['forms'][$form_id]['customErrorFunction'] = variable_get('clientside_validation_custom_error_function', '');
}
}
$form_settings_added[] = str_replace('-', '_', $form_id);
......
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