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

Commit

Permalink
Adding a reset event listener to reinit the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
1000hz committed Jul 16, 2016
1 parent d38456e commit 3f9369a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions js/tests/unit/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,28 @@ $(function () {

assert.equal($('#errors').text(), 'error', 'error is raised on source change')
})

QUnit.test('should reinitialize plugin on form reset', function (assert) {
var done = assert.async()
var form = '<form>'
+ '<div class="form-group">'
+ '<input type="text" data-minlength="6">'
+ '<div id="errors" class="help-block with-errors"></div>'
+ '</div>'
+ '</form>'

form = $(form)
.appendTo('#qunit-fixture')
.validator()

form.find('input').val('foo')

form.validator('validate')
form.trigger('reset')

window.setTimeout(function () {
assert.equal($('#errors').text(), '', 'error is cleared on form reset')
done()
}, 0)
})
})
11 changes: 11 additions & 0 deletions js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.onInput, this))
this.$element.on('submit.bs.validator', $.proxy(this.onSubmit, this))
this.$element.on('reset.bs.validator', $.proxy(this.onReset, this))

this.$element.find('[data-match]').each(function () {
var $this = $(this)
Expand Down Expand Up @@ -303,6 +304,16 @@
this.$btn.toggleClass('disabled', this.isIncomplete() || this.hasErrors())
}

Validator.prototype.onReset = function (e) {
var self = this
var options = this.options

window.setTimeout(function () {
self.destroy()
Plugin.call(self.$element, options)
}, 0)
}

Validator.prototype.defer = function ($el, callback) {
callback = $.proxy(callback, this, $el)
if (!this.options.delay) return callback()
Expand Down

0 comments on commit 3f9369a

Please sign in to comment.