diff --git a/js/tests/unit/validator.js b/js/tests/unit/validator.js index b92caaf..a731471 100644 --- a/js/tests/unit/validator.js +++ b/js/tests/unit/validator.js @@ -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 = $(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) + }) }) diff --git a/js/validator.js b/js/validator.js index 0c5d3b6..8e97177 100644 --- a/js/validator.js +++ b/js/validator.js @@ -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) @@ -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()