Skip to content

Commit 407f220

Browse files
committed
Unified way of dealing with async validations
1 parent 77a1b46 commit 407f220

File tree

4 files changed

+137
-14
lines changed

4 files changed

+137
-14
lines changed

src/main/async.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
*/
3+
(function ($, undefined) {
4+
5+
var disableFormSubmit = function () {
6+
return false;
7+
},
8+
HaltManager = {
9+
numHalted: 0,
10+
haltValidation: function($form) {
11+
this.numHalted++;
12+
$.formUtils.haltValidation = true;
13+
$form
14+
.unbind('submit', disableFormSubmit)
15+
.bind('submit', disableFormSubmit)
16+
.find('*[type="submit"]')
17+
.addClass('disabled')
18+
.attr('disabled', 'disabled');
19+
},
20+
unHaltValidation: function($form) {
21+
this.numHalted--;
22+
if (this.numHalted === 0) {
23+
$.formUtils.haltValidation = false;
24+
$form
25+
.unbind('submit', disableFormSubmit)
26+
.find('*[type="submit"]')
27+
.removeClass('disabled')
28+
.removeAttr('disabled', 'disabled');
29+
}
30+
}
31+
};
32+
33+
function AsyncValidation($form, $input) {
34+
this.$form = $form;
35+
this.$input = $input;
36+
this.reset();
37+
}
38+
39+
AsyncValidation.prototype.reset = function() {
40+
this.haltedFormValidation = false;
41+
this.hasRun = false;
42+
this.isRunning = false;
43+
this.result = undefined;
44+
};
45+
46+
AsyncValidation.prototype.run = function(eventContext, callback) {
47+
if (eventContext === 'keyup') {
48+
return null;
49+
} else if (this.isRunning) {
50+
if (!this.haltedFormValidation && eventContext === 'submit') {
51+
HaltManager.haltValidation();
52+
this.haltedFormValidation = true;
53+
}
54+
return null; // Waiting for result
55+
} else if(this.hasRun) {
56+
this.$input.one('keyup paste', this.reset.bind(this));
57+
return this.result;
58+
} else {
59+
console.log(eventContext);
60+
if (eventContext === 'submit') {
61+
HaltManager.haltValidation(this.$form);
62+
this.haltedFormValidation = true;
63+
}
64+
this.isRunning = true;
65+
this.$input
66+
.attr('disabled', 'disabled')
67+
.addClass('async-validation');
68+
this.$form.addClass('async-validation');
69+
70+
callback(function(result) {
71+
this.done(result);
72+
}.bind(this));
73+
74+
return null;
75+
}
76+
};
77+
78+
AsyncValidation.prototype.done = function(result) {
79+
this.result = result;
80+
this.hasRun = true;
81+
this.isRunning = false;
82+
this.$input
83+
.removeAttr('disabled')
84+
.removeClass('async-validation');
85+
this.$form.removeClass('async-validation');
86+
if (this.haltedFormValidation) {
87+
HaltManager.unHaltValidation(this.$form);
88+
this.$form.trigger('submit');
89+
} else {
90+
this.$input.trigger('validation.revalidate');
91+
}
92+
};
93+
94+
$.formUtils = $.extend($.formUtils || {}, {
95+
asyncValidation: function(validatorName, $input, $form) {
96+
// Return async validator attached to this input element
97+
// or create a new async validator and attach it to the input
98+
var asyncValidation,
99+
input = $input.get(0);
100+
101+
if (!input.asyncValidators) {
102+
input.asyncValidators = {};
103+
}
104+
105+
if (input.asyncValidators[validatorName]) {
106+
asyncValidation = input.asyncValidators[validatorName];
107+
} else {
108+
asyncValidation = new AsyncValidation($form, $input);
109+
input.asyncValidators[validatorName] = asyncValidation;
110+
}
111+
112+
return asyncValidation;
113+
}
114+
});
115+
116+
})(jQuery);

src/main/jquery-plugins.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,20 @@
172172
* @param {Object} [language] Optional, will override $.formUtils.LANG
173173
* @param {Object} [conf] Optional, will override the default settings
174174
* @param {Boolean} attachKeyupEvent Optional
175-
* @param {String} eventType
175+
* @param {String} eventContext
176176
* @return {jQuery}
177177
*/
178-
$.fn.validateInputOnBlur = function (language, conf, attachKeyupEvent, eventType) {
178+
$.fn.validateInputOnBlur = function (language, conf, attachKeyupEvent, eventContext) {
179179

180-
$.formUtils.eventType = eventType;
180+
$.formUtils.eventType = eventContext;
181181

182182
if ( this.willPostponeValidation() ) {
183183
// This validation has to be postponed
184184
var _self = this,
185185
postponeTime = this.valAttr('postpone') || 200;
186186

187187
window.postponedValidation = function () {
188-
_self.validateInputOnBlur(language, conf, attachKeyupEvent, eventType);
188+
_self.validateInputOnBlur(language, conf, attachKeyupEvent, eventContext);
189189
window.postponedValidation = false;
190190
};
191191

@@ -208,9 +208,19 @@
208208
language,
209209
conf,
210210
$form,
211-
eventType
211+
eventContext
212212
);
213213

214+
var reValidate = function() {
215+
$elem.validateInputOnBlur(language, conf, false, 'blur.revalidated');
216+
};
217+
218+
if (eventContext === 'blur') {
219+
$elem
220+
.unbind('validation.revalidate', reValidate)
221+
.one('validation.revalidate', reValidate);
222+
}
223+
214224
if (attachKeyupEvent) {
215225
$elem.removeKeyUpValidation();
216226
}
@@ -381,6 +391,8 @@
381391
'submit'
382392
);
383393

394+
console.log(result);
395+
384396
if (!result.isValid) {
385397
addErrorMessage(result.errorMsg, $elem);
386398
} else if (result.isValid && result.shouldChangeDisplay) {
@@ -408,10 +420,8 @@
408420
$.formUtils.isValidatingEntireForm = false;
409421

410422
// Validation failed
411-
if (!$.formUtils.haltValidation && errorInputs.length > 0) {
412-
423+
if (errorInputs.length > 0) {
413424
if (displayError) {
414-
415425
if (conf.errorMessagePosition === 'top') {
416426
$.formUtils.dialogs.setMessageInTopOfForm($form, errorMessages, conf, language);
417427
} else {
@@ -422,17 +432,14 @@
422432
if (conf.scrollToTopOnError) {
423433
$.formUtils.$win.scrollTop($form.offset().top - 20);
424434
}
425-
426435
}
427-
428-
return false;
429436
}
430437

431438
if (!displayError && $.formUtils.haltValidation) {
432439
$.formUtils.errorDisplayPreventedWhenHalted = true;
433440
}
434441

435-
return !$.formUtils.haltValidation;
442+
return errorInputs.length == 0 && !$.formUtils.haltValidation;
436443
};
437444

438445
/**

src/main/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
}
124124

125125
var valid = $form.isValid(conf.language, conf);
126-
126+
console.log(valid);
127127
if ($.formUtils.haltValidation) {
128128
// Validation got halted by one of the validators
129129
return stop();

src/main/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192

193193
if (eventContext !== 'keyup' || validator.validateOnKeyUp) {
194194
// A validator can prevent itself from getting triggered on keyup
195-
isValid = validator.validatorFunction(value, $elem, conf, language, $form);
195+
isValid = validator.validatorFunction(value, $elem, conf, language, $form, eventContext);
196196
}
197197

198198
if (!isValid) {

0 commit comments

Comments
 (0)