Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit 40f54eb

Browse files
Add support for deferred validators
1 parent dfa97d6 commit 40f54eb

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

js/validator.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
}
4141

4242
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"]'))
4950

5051
this.update()
5152

@@ -82,6 +83,7 @@
8283
disable: true,
8384
focus: true,
8485
custom: {},
86+
customDeferred: {},
8587
errors: {
8688
match: 'Does not match',
8789
minlength: 'Not long enough'
@@ -109,6 +111,18 @@
109111
}
110112
}
111113

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+
112126
Validator.prototype.update = function () {
113127
var self = this
114128

@@ -211,14 +225,21 @@
211225
}
212226
}, this))
213227

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) })
222243
} else deferred.resolve(errors)
223244

224245
return deferred.promise()
@@ -361,11 +382,12 @@
361382
this.$inputs
362383
.off('.bs.validator')
363384

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
369391

370392
return this
371393
}

0 commit comments

Comments
 (0)