diff --git a/dist/validator.js b/dist/validator.js index b881fea..39d5a83 100644 --- a/dist/validator.js +++ b/dist/validator.js @@ -1,5 +1,5 @@ /*! - * Validator v0.11.9 for Bootstrap 3, by @1000hz + * Validator v0.12.0 for Bootstrap 3, by @1000hz * Copyright 2017 Cina Saffary * Licensed under http://opensource.org/licenses/MIT * @@ -7,388 +7,420 @@ */ +function ($) { - 'use strict'; - - // VALIDATOR CLASS DEFINITION - // ========================== - - function getValue($el) { - return $el.is('[type="checkbox"]') ? $el.prop('checked') : - $el.is('[type="radio"]') ? !!$('[name="' + $el.attr('name') + '"]:checked').length : - $el.is('select[multiple]') ? ($el.val() || []).length : - $el.val() - } - - var Validator = function (element, options) { - this.options = options - this.validators = $.extend({}, Validator.VALIDATORS, options.custom) - this.$element = $(element) - this.$btn = $('button[type="submit"], input[type="submit"]') - .filter('[form="' + this.$element.attr('id') + '"]') - .add(this.$element.find('input[type="submit"], button[type="submit"]')) - - this.update() - - 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.reset, this)) - - this.$element.find('[data-match]').each(function () { - var $this = $(this) - var target = $this.attr('data-match') - - $(target).on('input.bs.validator', function (e) { - getValue($this) && $this.trigger('input.bs.validator') - }) - }) + 'use strict'; + + // VALIDATOR CLASS DEFINITION + // ========================== + + function getValue($el) { + + + var r = $el.is(':checkbox') ? $('[name="' + $el.attr('name') + '"]:checked').length : + $el.is('[type="radio"]') ? $('[name="' + $el.attr('name') + '"]:checked').length : + $el.is('select[multiple]') ? ($el.val() || []).length : + $el.val(); + + return r; + } - // run validators for fields with values, but don't clobber server-side errors - this.$inputs.filter(function () { - return getValue($(this)) && !$(this).closest('.has-error').length - }).trigger('focusout') - - this.$element.attr('novalidate', true) // disable automatic native validation - } - - Validator.VERSION = '0.11.9' - - Validator.INPUT_SELECTOR = ':input:not([type="hidden"], [type="submit"], [type="reset"], button)' - - Validator.FOCUS_OFFSET = 20 - - Validator.DEFAULTS = { - delay: 500, - html: false, - disable: true, - focus: true, - custom: {}, - errors: { - match: 'Does not match', - minlength: 'Not long enough' - }, - feedback: { - success: 'glyphicon-ok', - error: 'glyphicon-remove' + var Validator = function (element, options) { + this.options = options + this.validators = $.extend({}, Validator.VALIDATORS, options.custom) + this.$element = $(element) + this.$btn = $('button[type="submit"], input[type="submit"]') + .filter('[form="' + this.$element.attr('id') + '"]') + .add(this.$element.find('input[type="submit"], button[type="submit"]')) + + this.update() + + 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.reset, this)); + + this.$element.find('[data-match]').each(function () { + var $this = $(this) + var target = $this.attr('data-match') + + $(target).on('input.bs.validator', function (e) { + getValue($this) && $this.trigger('input.bs.validator') + }) + }); + + // run validators for fields with values, but don't clobber server-side errors + this.$inputs.filter(function () { + return getValue($(this)) && !$(this).closest('.has-error').length + }).trigger('focusout'); + + this.$element.attr('novalidate', true) // disable automatic native validation } - } - - Validator.VALIDATORS = { - 'native': function ($el) { - var el = $el[0] - if (el.checkValidity) { - return !el.checkValidity() && !el.validity.valid && (el.validationMessage || "error!") - } - }, - 'match': function ($el) { - var target = $el.attr('data-match') - return $el.val() !== $(target).val() && Validator.DEFAULTS.errors.match - }, - 'minlength': function ($el) { - var minlength = $el.attr('data-minlength') - return $el.val().length < minlength && Validator.DEFAULTS.errors.minlength + + Validator.VERSION = '0.11.9' + + Validator.INPUT_SELECTOR = ':input:not([type="hidden"], [type="submit"], [type="reset"], button)' + + Validator.FOCUS_OFFSET = 20 + + Validator.DEFAULTS = { + delay: 500, + html: false, + disable: true, + focus: true, + custom: {}, + errors: { + match: 'Does not match', + minlength: 'Not long enough' + }, + feedback: { + success: 'glyphicon-ok', + error: 'glyphicon-remove' + } } - } - Validator.prototype.update = function () { - var self = this + Validator.VALIDATORS = { + 'native': function ($el) { + var el = $el[0] + if (el.checkValidity) { + return !el.checkValidity() && !el.validity.valid && (el.validationMessage || "error!") + } + }, + 'match': function ($el) { + var target = $el.attr('data-match') + return $el.val() !== $(target).val() && Validator.DEFAULTS.errors.match + }, + 'minlength': function ($el) { + var minlength = $el.attr('data-minlength') + return $el.val().length < minlength && Validator.DEFAULTS.errors.minlength + } + } - this.$inputs = this.$element.find(Validator.INPUT_SELECTOR) - .add(this.$element.find('[data-validate="true"]')) - .not(this.$element.find('[data-validate="false"]') - .each(function () { self.clearErrors($(this)) }) - ) + Validator.prototype.update = function () { + var self = this - this.toggleSubmit() + this.$inputs = this.$element.find(Validator.INPUT_SELECTOR) + .add(this.$element.find('[data-validate="true"]')) + .not(this.$element.find('[data-validate="false"]') + .each(function () { + self.clearErrors($(this)) + }) + ) - return this - } + this.toggleSubmit() - Validator.prototype.onInput = function (e) { - var self = this - var $el = $(e.target) - var deferErrors = e.type !== 'focusout' + return this + } - if (!this.$inputs.is($el)) return + Validator.prototype.onInput = function (e) { + var self = this + var $el = $(e.target) + var deferErrors = e.type !== 'focusout' - this.validateInput($el, deferErrors).done(function () { - self.toggleSubmit() - }) - } + if (!this.$inputs.is($el)) + return - Validator.prototype.validateInput = function ($el, deferErrors) { - var value = getValue($el) - var prevErrors = $el.data('bs.validator.errors') + this.validateInput($el, deferErrors).done(function () { + self.toggleSubmit() + }) + } - if ($el.is('[type="radio"]')) $el = this.$element.find('input[name="' + $el.attr('name') + '"]') + Validator.prototype.validateInput = function ($el, deferErrors) { + var value = getValue($el) + var prevErrors = $el.data('bs.validator.errors') - var e = $.Event('validate.bs.validator', {relatedTarget: $el[0]}) - this.$element.trigger(e) - if (e.isDefaultPrevented()) return + if ($el.is('[type="radio"]')) + $el = this.$element.find('input[name="' + $el.attr('name') + '"]') - var self = this + if ($el.is(':checkbox')) { + $el = this.$element.find('input[name="' + $el.attr('name') + '"]'); + if (value) { + $el = $el.filter(':checked'); + } + } - return this.runValidators($el).done(function (errors) { - $el.data('bs.validator.errors', errors) + var e = $.Event('validate.bs.validator', {relatedTarget: $el[0]}) + this.$element.trigger(e) + if (e.isDefaultPrevented()) + return - errors.length - ? deferErrors ? self.defer($el, self.showErrors) : self.showErrors($el) - : self.clearErrors($el) + var self = this - if (!prevErrors || errors.toString() !== prevErrors.toString()) { - e = errors.length - ? $.Event('invalid.bs.validator', {relatedTarget: $el[0], detail: errors}) - : $.Event('valid.bs.validator', {relatedTarget: $el[0], detail: prevErrors}) + return this.runValidators($el).done(function (errors) { + $el.data('bs.validator.errors', errors) - self.$element.trigger(e) - } + errors.length + ? deferErrors ? self.defer($el, self.showErrors) : self.showErrors($el) + : self.clearErrors($el) - self.toggleSubmit() + if (!prevErrors || errors.toString() !== prevErrors.toString()) { + e = errors.length + ? $.Event('invalid.bs.validator', {relatedTarget: $el[0], detail: errors}) + : $.Event('valid.bs.validator', {relatedTarget: $el[0], detail: prevErrors}) - self.$element.trigger($.Event('validated.bs.validator', {relatedTarget: $el[0]})) - }) - } + self.$element.trigger(e) + } + self.toggleSubmit() - Validator.prototype.runValidators = function ($el) { - var errors = [] - var deferred = $.Deferred() + self.$element.trigger($.Event('validated.bs.validator', {relatedTarget: $el[0]})) + }) + } - $el.data('bs.validator.deferred') && $el.data('bs.validator.deferred').reject() - $el.data('bs.validator.deferred', deferred) - function getValidatorSpecificError(key) { - return $el.attr('data-' + key + '-error') + Validator.prototype.runValidators = function ($el) { + var errors = [] + var deferred = $.Deferred() + + $el.data('bs.validator.deferred') && $el.data('bs.validator.deferred').reject() + $el.data('bs.validator.deferred', deferred) + + function getValidatorSpecificError(key) { + return $el.attr('data-' + key + '-error') + } + + function getValidityStateError() { + var validity = $el[0].validity + return validity.typeMismatch ? $el.attr('data-type-error') + : validity.patternMismatch ? $el.attr('data-pattern-error') + : validity.stepMismatch ? $el.attr('data-step-error') + : validity.rangeOverflow ? $el.attr('data-max-error') + : validity.rangeUnderflow ? $el.attr('data-min-error') + : validity.valueMissing ? $el.attr('data-required-error') + : null + } + + function getGenericError() { + return $el.attr('data-error') + } + + function getErrorMessage(key) { + return getValidatorSpecificError(key) + || getValidityStateError() + || getGenericError() + } + + $.each(this.validators, $.proxy(function (key, validator) { + var error = null + if ((getValue($el) || $el.attr('required')) && + ($el.attr('data-' + key) !== undefined || key == 'native') && + (error = validator.call(this, $el))) { + error = getErrorMessage(key) || error + !~errors.indexOf(error) && errors.push(error) + } + }, this)) + + if (!errors.length && getValue($el) && $el.attr('data-remote')) { + this.defer($el, function () { + var data = {} + data[$el.attr('name')] = getValue($el) + $.get($el.attr('data-remote'), data) + .fail(function (jqXHR, textStatus, error) { + errors.push(getErrorMessage('remote') || error) + }) + .always(function () { + deferred.resolve(errors) + }) + }) + } else + deferred.resolve(errors) + + return deferred.promise() } - function getValidityStateError() { - var validity = $el[0].validity - return validity.typeMismatch ? $el.attr('data-type-error') - : validity.patternMismatch ? $el.attr('data-pattern-error') - : validity.stepMismatch ? $el.attr('data-step-error') - : validity.rangeOverflow ? $el.attr('data-max-error') - : validity.rangeUnderflow ? $el.attr('data-min-error') - : validity.valueMissing ? $el.attr('data-required-error') - : null + Validator.prototype.validate = function () { + var self = this + + $.when(this.$inputs.map(function (el) { + return self.validateInput($(this), false) + })).then(function () { + self.toggleSubmit() + self.focusError() + }) + + return this } - function getGenericError() { - return $el.attr('data-error') + Validator.prototype.focusError = function () { + if (!this.options.focus) + return + + var $input = this.$element.find(".has-error :input:first") + if ($input.length === 0) + return + + $('html, body').animate({scrollTop: $input.offset().top - Validator.FOCUS_OFFSET}, 250) + $input.focus() } - function getErrorMessage(key) { - return getValidatorSpecificError(key) - || getValidityStateError() - || getGenericError() + Validator.prototype.showErrors = function ($el) { + var method = this.options.html ? 'html' : 'text' + var errors = $el.data('bs.validator.errors') + var $group = $el.closest('.form-group') + var $block = $group.find('.help-block.with-errors') + var $feedback = $group.find('.form-control-feedback') + + if (!errors.length) + return + + errors = $('