|
40 | 40 | } |
41 | 41 |
|
42 | 42 | var Validator = function (element, options) { |
43 | | - this.options = options |
44 | | - this.validators = $.extend({}, Validator.VALIDATORS, options.custom) |
45 | | - this.$element = $(element) |
46 | | - this.$btn = $('button[type="submit"], input[type="submit"]') |
47 | | - .filter('[form="' + this.$element.attr('id') + '"]') |
48 | | - .add(this.$element.find('input[type="submit"], button[type="submit"]')) |
| 43 | + this.options = options |
| 44 | + this.validators = $.extend({}, Validator.VALIDATORS, options.custom) |
| 45 | + this.deferredValidators = $.extend({}, Validator.DEFERRED_VALIDATORS, options.customDeferred) |
| 46 | + this.$element = $(element) |
| 47 | + this.$btn = $('button[type="submit"], input[type="submit"]') |
| 48 | + .filter('[form="' + this.$element.attr('id') + '"]') |
| 49 | + .add(this.$element.find('input[type="submit"], button[type="submit"]')) |
49 | 50 |
|
50 | 51 | this.update() |
51 | 52 |
|
|
82 | 83 | disable: true, |
83 | 84 | focus: true, |
84 | 85 | custom: {}, |
| 86 | + customDeferred: {}, |
85 | 87 | errors: { |
86 | 88 | match: 'Does not match', |
87 | 89 | minlength: 'Not long enough' |
|
109 | 111 | } |
110 | 112 | } |
111 | 113 |
|
| 114 | + Validator.DEFERRED_VALIDATORS = { |
| 115 | + 'remote': function ($el) { |
| 116 | + var deferred = $.Deferred() |
| 117 | + var data = {} |
| 118 | + data[$el.attr('name')] = getValue($el) |
| 119 | + $.get($el.attr('data-remote'), data) |
| 120 | + .fail(function (jqXHR, textStatus, error) { deferred.reject(error) }) |
| 121 | + .done(function (jqXHR, textStatus) { deferred.resolve() }) |
| 122 | + return deferred |
| 123 | + } |
| 124 | + } |
| 125 | + |
112 | 126 | Validator.prototype.update = function () { |
113 | 127 | var self = this |
114 | 128 |
|
|
211 | 225 | } |
212 | 226 | }, this)) |
213 | 227 |
|
214 | | - if (!errors.length && getValue($el) && $el.attr('data-remote')) { |
215 | | - this.defer($el, function () { |
216 | | - var data = {} |
217 | | - data[$el.attr('name')] = getValue($el) |
218 | | - $.get($el.attr('data-remote'), data) |
219 | | - .fail(function (jqXHR, textStatus, error) { errors.push(getErrorMessage('remote') || error) }) |
220 | | - .always(function () { deferred.resolve(errors)}) |
221 | | - }) |
| 228 | + if (!errors.length && getValue($el)) { |
| 229 | + var self = this |
| 230 | + var deferredErrors = [] |
| 231 | + $.each(self.deferredValidators, $.proxy(function (key, validator) { |
| 232 | + if ($el.attr('data-' + key) === undefined) { |
| 233 | + return |
| 234 | + } |
| 235 | + deferredErrors.push(validator.call(self, $el) |
| 236 | + .fail(function (error) { |
| 237 | + error = getErrorMessage(key) || error |
| 238 | + !~errors.indexOf(error) && errors.push(error) |
| 239 | + })) |
| 240 | + }, self)) |
| 241 | + $.when.apply(deferredErrors) |
| 242 | + .always(function () { deferred.resolve(errors) }) |
222 | 243 | } else deferred.resolve(errors) |
223 | 244 |
|
224 | 245 | return deferred.promise() |
|
361 | 382 | this.$inputs |
362 | 383 | .off('.bs.validator') |
363 | 384 |
|
364 | | - this.options = null |
365 | | - this.validators = null |
366 | | - this.$element = null |
367 | | - this.$btn = null |
368 | | - this.$inputs = null |
| 385 | + this.options = null |
| 386 | + this.validators = null |
| 387 | + this.deferredValidators = null |
| 388 | + this.$element = null |
| 389 | + this.$btn = null |
| 390 | + this.$inputs = null |
369 | 391 |
|
370 | 392 | return this |
371 | 393 | } |
|
0 commit comments