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

Commit

Permalink
v0.11.0 🔙🔛🔝
Browse files Browse the repository at this point in the history
  • Loading branch information
1000hz committed Jul 15, 2016
1 parent c264f9b commit 477463b
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 86 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
Changelog
=========
### 0.11.0
###### BREAKING CHANGES:
* Custom validators are now expected to return an error string if the field is invalid.
* The errors option has been removed. Override `$.fn.validator.Constructor.DEFAULTS.errors` if you want to change the default `match` and `minlength` errors.
* The validator no longer skips disabled/invisible fields. If you want this behavior back, add `$.fn.validator.Constructor.INPUT_SELECTOR += ':enabled:visible'` to your code. ([#115](https://github.com/1000hz/bootstrap-validator/issues/115)) ([#134](https://github.com/1000hz/bootstrap-validator/issues/134)) ([#317](https://github.com/1000hz/bootstrap-validator/issues/317))

###### Enhancements:
* Added support for distinct custom errors for the standard HTML5 attribute validators. No more being stuck with `data-native-error=""` for all of them. ([#222](https://github.com/1000hz/bootstrap-validator/issues/222)) ([#241](https://github.com/1000hz/bootstrap-validator/issues/241)) ([#285](https://github.com/1000hz/bootstrap-validator/issues/285))
* Added a `.validator('update')` method to refresh the set of fields that will be validated ([#306](https://github.com/1000hz/bootstrap-validator/issues/306))
* Added support of `data-validate="true|false"` on inputs to force validation of that field
* Immediately validating fields that already have a value upon validator initialization ([#350](https://github.com/1000hz/bootstrap-validator/issues/350))

###### Bugfixes:
* Fixed a bug in Safari where `element.checkValidity()` was returning stale values ([#293](https://github.com/1000hz/bootstrap-validator/issues/293))
* Fixed a bug where spaces at the end of inputs were being trimmed off before being run through validators ([#338](https://github.com/1000hz/bootstrap-validator/issues/338))
* Custom validators no longer leak to other instances of Validator. ([#176](https://github.com/1000hz/bootstrap-validator/issues/176))
* Scrolling with `focus: true` option is now triggered on `$('html, body')` instead of `$(document.body)` for better cross-browser support ([#282](https://github.com/1000hz/bootstrap-validator/issues/282))
* Removed (value == previousValue => skip) optimization. It was breaking the match validator and wasn't improving perf that much. ([#316](https://github.com/1000hz/bootstrap-validator/issues/316)) ([#340](https://github.com/1000hz/bootstrap-validator/issues/340))
* Added `$.fn.validator.Constructor.VERSION` property for parity with core Bootstrap plugins

###### Docs Changes:
* Docs: Added an Overview section which calls out that whatever conventions apply to Bootstrap's core plugins also apply here
* Docs: Added a callout blurb about the standard attribute validators
* Docs: Added a "Validated fields" section to document the Validator.INPUT_SELECTOR field
* Docs: Removed `$()` from method headers, which was confusing some people ([#202](https://github.com/1000hz/bootstrap-validator/issues/202))


### 0.10.2
* Fixed a bug with the form still submitting even with errors when using the `disable: false` option. ([#310](https://github.com/1000hz/bootstrap-validator/issues/310))
* Fixed a bug with the error field not being focused when using the `disable: false` option. ([#310](https://github.com/1000hz/bootstrap-validator/issues/310))
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Cina Saffary
Copyright (c) 2016 Cina Saffary

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ Lastly, don't pollute your patch branch with any unrelated changes.
Thanks to [@mdo](https://github.com/mdo) and [@fat](https://github.com/fat) for [Bootstrap](http://getbootstrap.com). <3

## Copyright and license
Copyright 2015 Cina Saffary under the MIT license.
Copyright 2016 Cina Saffary under the MIT license.
102 changes: 64 additions & 38 deletions dist/validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Validator v0.10.2 for Bootstrap 3, by @1000hz
* Validator v0.11.0 for Bootstrap 3, by @1000hz
* Copyright 2016 Cina Saffary
* Licensed under http://opensource.org/licenses/MIT
*
Expand All @@ -15,29 +15,20 @@
function getValue($el) {
return $el.is('[type="checkbox"]') ? $el.prop('checked') :
$el.is('[type="radio"]') ? !!$('[name="' + $el.attr('name') + '"]:checked').length :
$.trim($el.val())
$el.val()
}

var Validator = function (element, options) {
this.options = options
this.$element = $(element)
this.$inputs = this.$element.find(Validator.INPUT_SELECTOR)
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.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"]'))

options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors)
this.update()

for (var custom in options.custom) {
if (!options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom)
}

$.extend(Validator.VALIDATORS, options.custom)

this.$element.attr('novalidate', true) // disable automatic native validation
this.toggleSubmit()

this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', Validator.INPUT_SELECTOR, $.proxy(this.onInput, this))
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.find('[data-match]').each(function () {
Expand All @@ -48,9 +39,16 @@
getValue($this) && $this.trigger('input.bs.validator')
})
})

this.$inputs.filter(function () { return getValue($(this)) }).trigger('focusout')

this.$element.attr('novalidate', true) // disable automatic native validation
this.toggleSubmit()
}

Validator.INPUT_SELECTOR = ':input:not([type="submit"], button):enabled:visible'
Validator.VERSION = '0.11.0'

Validator.INPUT_SELECTOR = ':input:not([type="hidden"], [type="submit"], button)'

Validator.FOCUS_OFFSET = 20

Expand All @@ -73,36 +71,45 @@
Validator.VALIDATORS = {
'native': function ($el) {
var el = $el[0]
return el.checkValidity ? el.checkValidity() : true
if (el.checkValidity) {
return !el.checkValidity() && !el.validity.valid && (el.validationMessage || "error!")
}
},
'match': function ($el) {
var target = $el.data('match')
return !$el.val() || $el.val() === $(target).val()
return $el.val() !== $(target).val() && Validator.DEFAULTS.errors.match
},
'minlength': function ($el) {
var minlength = $el.data('minlength')
return !$el.val() || $el.val().length >= minlength
return $el.val().length < minlength && Validator.DEFAULTS.errors.minlength
}
}

Validator.prototype.update = function () {
this.$inputs = this.$element.find(Validator.INPUT_SELECTOR)
.add(this.$element.find('[data-validate="true"]'))
.not(this.$element.find('[data-validate="false"]'))

return this
}

Validator.prototype.onInput = function (e) {
var self = this
var $el = $(e.target)
var deferErrors = e.type !== 'focusout'

if (!this.$inputs.is($el)) return

this.validateInput($el, deferErrors).done(function () {
self.toggleSubmit()
})
}

Validator.prototype.validateInput = function ($el, deferErrors) {
var value = getValue($el)
var prevValue = $el.data('bs.validator.previous')
var prevErrors = $el.data('bs.validator.errors')
var errors

if (prevValue === value) return $.Deferred().resolve()
else $el.data('bs.validator.previous', value)

if ($el.is('[type="radio"]')) $el = this.$element.find('input[name="' + $el.attr('name') + '"]')

var e = $.Event('validate.bs.validator', {relatedTarget: $el[0]})
Expand Down Expand Up @@ -136,23 +143,41 @@
Validator.prototype.runValidators = function ($el) {
var errors = []
var deferred = $.Deferred()
var options = this.options

$el.data('bs.validator.deferred') && $el.data('bs.validator.deferred').reject()
$el.data('bs.validator.deferred', deferred)

function getErrorMessage(key) {
function getValidatorSpecificError(key) {
return $el.data(key + '-error')
|| $el.data('error')
|| key == 'native' && $el[0].validationMessage
|| options.errors[key]
}

$.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
function getValidityStateError() {
var validity = $el[0].validity
return validity.typeMismatch ? $el.data('type-error')
: validity.patternMismatch ? $el.data('pattern-error')
: validity.stepMismatch ? $el.data('step-error')
: validity.rangeOverflow ? $el.data('max-error')
: validity.rangeUnderflow ? $el.data('min-error')
: validity.valueMissing ? $el.data('required-error')
: null
}

function getGenericError() {
return $el.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.data(key) || key == 'native') &&
!validator.call(this, $el)) {
var error = getErrorMessage(key)
(error = validator.call(this, $el))) {
error = getErrorMessage(key) || error
!~errors.indexOf(error) && errors.push(error)
}
}, this))
Expand Down Expand Up @@ -189,7 +214,7 @@
var $input = $(".has-error:first :input")
if ($input.length === 0) return

$(document.body).animate({scrollTop: $input.offset().top - Validator.FOCUS_OFFSET}, 250)
$('html, body').animate({scrollTop: $input.offset().top - Validator.FOCUS_OFFSET}, 250)
$input.focus()
}

Expand Down Expand Up @@ -241,7 +266,8 @@

Validator.prototype.isIncomplete = function () {
function fieldIncomplete() {
return !getValue($(this))
var value = getValue($(this))
return !(typeof value == "string" ? $.trim(value) : value)
}

return !!this.$inputs.filter('[required]').filter(fieldIncomplete).length
Expand Down Expand Up @@ -274,7 +300,7 @@

this.$inputs
.off('.bs.validator')
.removeData(['bs.validator.errors', 'bs.validator.deferred', 'bs.validator.previous'])
.removeData(['bs.validator.errors', 'bs.validator.deferred'])
.each(function () {
var $this = $(this)
var timeout = $this.data('bs.validator.timeout')
Expand Down
Loading

0 comments on commit 477463b

Please sign in to comment.