|
8 | 8 | (c) 2011 Victor Jonsson, Sweden.
|
9 | 9 | Dual licensed under the MIT or GPL Version 2 licenses
|
10 | 10 |
|
11 |
| - $version 1.0 |
| 11 | + $version 1.1 |
12 | 12 | */
|
13 | 13 | (function($) {
|
14 | 14 | $.extend($.fn, {
|
| 15 | + |
| 16 | + /** |
| 17 | + * @param attrName |
| 18 | + * @return jQuery |
| 19 | + */ |
| 20 | + showHelp : function(attrName) { |
| 21 | + if(typeof attrName == 'undefined') |
| 22 | + attrName = 'data-help'; |
| 23 | + |
| 24 | + $(this).each(function() { |
| 25 | + var help = $(this).attr(attrName); |
| 26 | + if(help) { |
| 27 | + $(this) |
| 28 | + .focus(function() { |
| 29 | + var span = $('<span />') |
| 30 | + .addClass('jquery_form_help') |
| 31 | + .text(help) |
| 32 | + .hide() |
| 33 | + .fadeIn(); |
| 34 | + |
| 35 | + $(this).after(span); |
| 36 | + }) |
| 37 | + .blur(function() { |
| 38 | + $(this).parent().find('.jquery_form_help') |
| 39 | + .fadeOut('slow', function() { |
| 40 | + $(this).remove(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + } |
| 44 | + }); |
| 45 | + return $(this); |
| 46 | + }, |
| 47 | + |
| 48 | + /** |
| 49 | + * Function that validates the value of given input and shows |
| 50 | + * error message in a span element that is appended to the parent |
| 51 | + * element |
| 52 | + * @param language |
| 53 | + * @param settings |
| 54 | + * @return jQuery |
| 55 | + */ |
| 56 | + doValidate : function(language, settings) { |
| 57 | + var config = { |
| 58 | + validationRuleAttribute : 'data-validation', |
| 59 | + errorElementClass : 'error', // Class that will be put on elements which value is invalid |
| 60 | + borderColorOnError : 'red' |
| 61 | + }; |
| 62 | + |
| 63 | + if (settings) |
| 64 | + $.extend(config, settings); |
| 65 | + if (language) |
| 66 | + $.extend(language, jQueryFormUtils.LANG); |
| 67 | + else |
| 68 | + language = jQueryFormUtils.LANG; |
| 69 | + |
| 70 | + if (jQueryFormUtils.defaultBorderColor == null && $(this).attr('type') == 'text') |
| 71 | + jQueryFormUtils.defaultBorderColor = $(this).css('border-color'); |
| 72 | + |
| 73 | + // Remove possible error style applied by previous validation |
| 74 | + $(this) |
| 75 | + .removeClass(config.errorElementClass) |
| 76 | + .parent() |
| 77 | + .find('.jquery_form_error_message').remove(); |
| 78 | + |
| 79 | + var validation = jQueryFormUtils.validateInput($(this), language, config.validationRuleAttribute); |
| 80 | + |
| 81 | + if(validation !== true) { |
| 82 | + $(this) |
| 83 | + .addClass(config.errorElementClass) |
| 84 | + .parent() |
| 85 | + .append('<span class="jquery_form_error_message">'+validation+'</span>'); |
| 86 | + |
| 87 | + if(config.borderColorOnError != '') |
| 88 | + $(this).css('border-color', config.borderColorOnError); |
| 89 | + } |
| 90 | + else { |
| 91 | + if(config.borderColorOnError != '') |
| 92 | + $(this).css('border-color', jQueryFormUtils.defaultBorderColor); |
| 93 | + } |
| 94 | + |
| 95 | + return $(this); |
| 96 | + }, |
| 97 | + |
| 98 | + /** |
| 99 | + * Function that validate all inputs in a form |
| 100 | + * @param language |
| 101 | + * @param settings |
| 102 | + */ |
15 | 103 | validate : function(language, settings) {
|
16 | 104 |
|
17 | 105 | /*
|
|
66 | 154 | /** Error messages for this validation */
|
67 | 155 | var errorMessages = [];
|
68 | 156 |
|
69 |
| - /** Input elements whitch value wasnt valid */ |
| 157 | + /** Input elements which value was not valid */ |
70 | 158 | var errorInputs = [];
|
71 | 159 |
|
72 | 160 | /** Form instance */
|
|
77 | 165 | //
|
78 | 166 | $(this).find('input[type=radio]').each(function() {
|
79 | 167 | var validationRule = $(this).attr(config.validationRuleAttribute);
|
80 |
| - if (typeof validationRule != 'undefined' && validationRule != null) { |
81 |
| - if (validationRule == 'required') { |
82 |
| - var radioButtonName = $(this).attr('name'); |
83 |
| - var isChecked = false; |
84 |
| - form.find('input[name=' + radioButtonName + ']').each(function() { |
85 |
| - if ($(this).is(':checked')) |
86 |
| - isChecked = true; |
87 |
| - }); |
88 |
| - if (!isChecked) { |
89 |
| - errorMessages.push(language.requiredFields); |
90 |
| - } |
| 168 | + if (typeof validationRule != 'undefined' && validationRule == 'required') { |
| 169 | + var radioButtonName = $(this).attr('name'); |
| 170 | + var isChecked = false; |
| 171 | + form.find('input[name=' + radioButtonName + ']').each(function() { |
| 172 | + if ($(this).is(':checked')) |
| 173 | + isChecked = true; |
| 174 | + }); |
| 175 | + if (!isChecked) { |
| 176 | + errorMessages.push(language.requiredFields); |
| 177 | + errorInputs.push($(this)); |
| 178 | + $(this).attr('data-error', language.requiredFields); |
91 | 179 | }
|
92 | 180 | }
|
93 | 181 | });
|
|
128 | 216 | //
|
129 | 217 | // Remove possible error messages from last validation
|
130 | 218 | //
|
131 |
| - if (config.errorMessagePosition == 'top') |
132 |
| - $('.' + config.errorMessageClass).remove(); |
133 |
| - else |
134 |
| - $('.jquery_form_error_message').remove(); |
| 219 | + $('.' + config.errorMessageClass).remove(); |
| 220 | + $('.jquery_form_error_message').remove(); |
135 | 221 |
|
136 | 222 |
|
137 | 223 | //
|
@@ -459,7 +545,6 @@ jQueryFormUtils.validateDomain = function(val) {
|
459 | 545 | * @return string|true
|
460 | 546 | */
|
461 | 547 | jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form) {
|
462 |
| - |
463 | 548 | var value = jQuery.trim(el.val());
|
464 | 549 | var validationRules = el.attr(validationRuleAttr);
|
465 | 550 |
|
@@ -568,7 +653,7 @@ jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form)
|
568 | 653 | }
|
569 | 654 |
|
570 | 655 | // confirmation
|
571 |
| - if (validationRules.indexOf('validate_confirmation') > -1) { |
| 656 | + if (validationRules.indexOf('validate_confirmation') > -1 && typeof(form) != 'undefined') { |
572 | 657 | var conf = '';
|
573 | 658 | var confInput = form.find('input[name=' + el.attr('name') + '_confirmation]').eq(0);
|
574 | 659 | if (confInput)
|
|
0 commit comments