|
5 | 5 |
|
6 | 6 | 'use strict';
|
7 | 7 |
|
8 |
| - var errorDialogs = { |
| 8 | + var dialogs = { |
9 | 9 |
|
| 10 | + resolveErrorMessage: function($elem, validator, validatorName, conf, language) { |
| 11 | + var errorMsgAttr = conf.validationErrorMsgAttribute + '-' + validatorName.replace('validate_', ''), |
| 12 | + validationErrorMsg = $elem.attr(errorMsgAttr); |
| 13 | + |
| 14 | + if (!validationErrorMsg) { |
| 15 | + validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute); |
| 16 | + if (!validationErrorMsg) { |
| 17 | + if (typeof validator.errorMessageKey !== 'function') { |
| 18 | + validationErrorMsg = language[validator.errorMessageKey]; |
| 19 | + } |
| 20 | + else { |
| 21 | + validationErrorMsg = language[validator.errorMessageKey(conf)]; |
| 22 | + } |
| 23 | + if (!validationErrorMsg) { |
| 24 | + validationErrorMsg = validator.errorMessage; |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + return validationErrorMsg; |
| 29 | + }, |
10 | 30 | getParentContainer: function ($elem) {
|
11 | 31 | if ($elem.valAttr('error-msg-container')) {
|
12 | 32 | return $($elem.valAttr('error-msg-container'));
|
|
21 | 41 | return $parent;
|
22 | 42 | }
|
23 | 43 | },
|
24 |
| - applyErrorStyling: function ($elem, conf) { |
25 |
| - $elem |
| 44 | + applyInputErrorStyling: function ($input, conf) { |
| 45 | + $input |
26 | 46 | .addClass(conf.errorElementClass)
|
27 | 47 | .removeClass('valid');
|
28 | 48 |
|
29 |
| - this.getParentContainer($elem) |
| 49 | + this.getParentContainer($input) |
30 | 50 | .addClass(conf.inputParentClassOnError)
|
31 | 51 | .removeClass(conf.inputParentClassOnSuccess);
|
32 | 52 |
|
33 | 53 | if (conf.borderColorOnError !== '') {
|
34 |
| - $elem.css('border-color', conf.borderColorOnError); |
| 54 | + $input.css('border-color', conf.borderColorOnError); |
35 | 55 | }
|
36 | 56 | },
|
37 |
| - removeErrorStyling: function ($elem, conf) { |
38 |
| - $elem.each(function () { |
39 |
| - var $this = $(this); |
| 57 | + applyInputSuccessStyling: function($input, conf) { |
| 58 | + $input.addClass('valid'); |
| 59 | + this.getParentContainer($input) |
| 60 | + .addClass(conf.inputParentClassOnSuccess); |
| 61 | + }, |
| 62 | + removeInputStylingAndMessage: function($input, conf) { |
40 | 63 |
|
41 |
| - errorDialogs.setInlineErrorMessage($this, '', conf, conf.errorMessagePosition); |
| 64 | + // Reset input css |
| 65 | + $input |
| 66 | + .removeClass('valid') |
| 67 | + .removeClass(conf.errorElementClass) |
| 68 | + .css('border-color', ''); |
42 | 69 |
|
43 |
| - $this |
44 |
| - .removeClass('valid') |
45 |
| - .removeClass(conf.errorElementClass) |
46 |
| - .css('border-color', ''); |
| 70 | + var $parentContainer = dialogs.getParentContainer($input); |
| 71 | + |
| 72 | + // Reset parent css |
| 73 | + $parentContainer |
| 74 | + .removeClass(conf.inputParentClassOnError) |
| 75 | + .removeClass(conf.inputParentClassOnSuccess); |
47 | 76 |
|
48 |
| - errorDialogs.getParentContainer($this) |
49 |
| - .removeClass(conf.inputParentClassOnError) |
50 |
| - .removeClass(conf.inputParentClassOnSuccess) |
51 |
| - .find('.' + conf.errorMessageClass) // remove inline span holding error message |
| 77 | + // Remove possible error message |
| 78 | + if (typeof conf.inlineErrorMessageCallback === 'function') { |
| 79 | + var $errorMessage = conf.inlineErrorMessageCallback($input, conf); |
| 80 | + if ($errorMessage) { |
| 81 | + $errorMessage.html(''); |
| 82 | + } |
| 83 | + } else { |
| 84 | + $parentContainer |
| 85 | + .find('.' + conf.errorMessageClass) |
52 | 86 | .remove();
|
| 87 | + } |
| 88 | + |
| 89 | + }, |
| 90 | + removeAllMessagesAndStyling: function($form, conf) { |
| 91 | + |
| 92 | + // Remove error messages in top of form |
| 93 | + if (typeof conf.submitErrorMessageCallback === 'function') { |
| 94 | + var $errorMessagesInTopOfForm = conf.submitErrorMessageCallback($form, conf); |
| 95 | + if ($errorMessagesInTopOfForm) { |
| 96 | + $errorMessagesInTopOfForm.html(''); |
| 97 | + } |
| 98 | + } else { |
| 99 | + $form.find('.' + conf.errorMessageClass + '.alert').remove(); |
| 100 | + } |
| 101 | + |
| 102 | + // Remove input css/messages |
| 103 | + $form.find('.' + conf.errorElementClass + ',.valid').each(function() { |
| 104 | + dialogs.removeInputStylingAndMessage($(this), conf); |
53 | 105 | });
|
54 | 106 | },
|
55 |
| - setInlineErrorMessage: function ($input, errorMsg, conf, $messageContainer) { |
| 107 | + setInlineMessage: function ($input, errorMsg, conf) { |
| 108 | + |
| 109 | + this.applyInputErrorStyling($input, conf); |
| 110 | + |
56 | 111 | var custom = document.getElementById($input.attr('name') + '_err_msg'),
|
| 112 | + $messageContainer = false, |
57 | 113 | setErrorMessage = function ($elem) {
|
58 | 114 | $.formUtils.$win.trigger('validationErrorDisplay', [$input, $elem]);
|
59 | 115 | $elem.html(errorMsg);
|
60 | 116 | },
|
| 117 | + addErrorToMessageContainer = function() { |
| 118 | + var $found = false; |
| 119 | + $messageContainer.find('.' + conf.errorMessageClass).each(function () { |
| 120 | + if (this.inputReferer === $input[0]) { |
| 121 | + $found = $(this); |
| 122 | + return false; |
| 123 | + } |
| 124 | + }); |
| 125 | + console.log($found); |
| 126 | + if ($found) { |
| 127 | + if (!errorMsg) { |
| 128 | + $found.remove(); |
| 129 | + } else { |
| 130 | + setErrorMessage($found); |
| 131 | + } |
| 132 | + } else if(errorMsg !== '') { |
| 133 | + $message = $('<div class="' + conf.errorMessageClass + ' alert"></div>'); |
| 134 | + setErrorMessage($message); |
| 135 | + $message[0].inputReferer = $input[0]; |
| 136 | + $messageContainer.prepend($message); |
| 137 | + } |
| 138 | + }, |
61 | 139 | $message;
|
62 | 140 |
|
63 | 141 | if (custom) {
|
| 142 | + // Todo: remove in 3.0 |
64 | 143 | $.formUtils.warn('Using deprecated element reference ' + custom.id);
|
65 | 144 | $messageContainer = $(custom);
|
66 |
| - } else if (typeof $messageContainer === 'function') { |
67 |
| - $messageContainer = $messageContainer($input, errorMsg, conf); |
68 |
| - } |
69 |
| - |
70 |
| - if (typeof $messageContainer === 'object') { |
71 |
| - var $found = false; |
72 |
| - $messageContainer.find('.' + conf.errorMessageClass).each(function () { |
73 |
| - if (this.inputReferer === $input[0]) { |
74 |
| - $found = $(this); |
75 |
| - return false; |
76 |
| - } |
77 |
| - }); |
78 |
| - if ($found) { |
79 |
| - if (!errorMsg) { |
80 |
| - $found.remove(); |
81 |
| - } else { |
82 |
| - setErrorMessage($found); |
83 |
| - } |
84 |
| - } else if(errorMsg !== '') { |
85 |
| - $message = $('<div class="' + conf.errorMessageClass + ' alert"></div>'); |
86 |
| - setErrorMessage($message); |
87 |
| - $message[0].inputReferer = $input[0]; |
88 |
| - $messageContainer.prepend($message); |
| 145 | + addErrorToMessageContainer(); |
| 146 | + } else if (typeof conf.inlineErrorMessageCallback === 'function') { |
| 147 | + $messageContainer = conf.inlineErrorMessageCallback($input, conf); |
| 148 | + if (!$messageContainer) { |
| 149 | + // Error display taken care of by inlineErrorMessageCallback |
| 150 | + return; |
89 | 151 | }
|
90 |
| - } |
91 |
| - else { |
| 152 | + addErrorToMessageContainer(); |
| 153 | + } else { |
92 | 154 | var $parent = this.getParentContainer($input);
|
93 | 155 | $message = $parent.find('.' + conf.errorMessageClass + '.help-block');
|
94 |
| - |
95 | 156 | if ($message.length === 0) {
|
96 | 157 | $message = $('<span></span>').addClass('help-block').addClass(conf.errorMessageClass);
|
97 | 158 | $message.appendTo($parent);
|
98 | 159 | }
|
99 |
| - |
100 | 160 | setErrorMessage($message);
|
101 | 161 | }
|
102 | 162 | },
|
103 |
| - setTemplateMessage: function ($form, title, errorMessages, conf) { |
104 |
| - var messages = conf.errorMessageTemplate.messages.replace(/\{errorTitle\}/g, title), |
105 |
| - fields = [], |
106 |
| - container; |
| 163 | + setMessageInTopOfForm: function ($form, errorMessages, conf, lang) { |
| 164 | + var view = '<div class="{errorMessageClass} alert alert-danger">'+ |
| 165 | + '<strong>{errorTitle}</strong>'+ |
| 166 | + '<ul>{fields}</ul>'+ |
| 167 | + '</div>', |
| 168 | + $container = false; |
| 169 | + |
| 170 | + if (typeof conf.submitErrorMessageCallback === 'function') { |
| 171 | + $container = conf.submitErrorMessageCallback($form, errorMessages, conf); |
| 172 | + console.log($container); |
| 173 | + if (!$container) { |
| 174 | + // message display taken care of by callback |
| 175 | + return; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + var viewParams = { |
| 180 | + errorTitle: lang.errorTitle, |
| 181 | + fields: '', |
| 182 | + errorMessageClass: conf.errorMessageClass |
| 183 | + }; |
107 | 184 |
|
108 | 185 | $.each(errorMessages, function (i, msg) {
|
109 |
| - fields.push(conf.errorMessageTemplate.field.replace(/\{msg\}/g, msg)); |
| 186 | + viewParams.fields += '<li>'+msg+'</li>'; |
110 | 187 | });
|
111 | 188 |
|
112 |
| - messages = messages.replace(/\{fields\}/g, fields.join('')); |
113 |
| - container = conf.errorMessageTemplate.container.replace(/\{errorMessageClass\}/g, conf.errorMessageClass); |
114 |
| - container = container.replace(/\{messages\}/g, messages); |
115 |
| - $form.children().eq(0).before(container); |
| 189 | + $.each(viewParams, function(param, value) { |
| 190 | + view = view.replace('{'+param+'}', value); |
| 191 | + }); |
| 192 | + |
| 193 | + if ($container) { |
| 194 | + $container.html(view); |
| 195 | + } else { |
| 196 | + $form.children().eq(0).before($(view)); |
| 197 | + } |
116 | 198 | }
|
117 | 199 | };
|
118 | 200 |
|
119 | 201 | $.formUtils = $.extend($.formUtils || {}, {
|
120 |
| - errorDialogs: errorDialogs |
| 202 | + dialogs: dialogs |
121 | 203 | });
|
122 | 204 |
|
123 | 205 | })(jQuery);
|
0 commit comments