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

Commit 477463b

Browse files
committed
v0.11.0 🔙🔛🔝
1 parent c264f9b commit 477463b

File tree

9 files changed

+165
-86
lines changed

9 files changed

+165
-86
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
Changelog
22
=========
3+
### 0.11.0
4+
###### BREAKING CHANGES:
5+
* Custom validators are now expected to return an error string if the field is invalid.
6+
* The errors option has been removed. Override `$.fn.validator.Constructor.DEFAULTS.errors` if you want to change the default `match` and `minlength` errors.
7+
* 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))
8+
9+
###### Enhancements:
10+
* 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))
11+
* Added a `.validator('update')` method to refresh the set of fields that will be validated ([#306](https://github.com/1000hz/bootstrap-validator/issues/306))
12+
* Added support of `data-validate="true|false"` on inputs to force validation of that field
13+
* Immediately validating fields that already have a value upon validator initialization ([#350](https://github.com/1000hz/bootstrap-validator/issues/350))
14+
15+
###### Bugfixes:
16+
* Fixed a bug in Safari where `element.checkValidity()` was returning stale values ([#293](https://github.com/1000hz/bootstrap-validator/issues/293))
17+
* 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))
18+
* Custom validators no longer leak to other instances of Validator. ([#176](https://github.com/1000hz/bootstrap-validator/issues/176))
19+
* 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))
20+
* 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))
21+
* Added `$.fn.validator.Constructor.VERSION` property for parity with core Bootstrap plugins
22+
23+
###### Docs Changes:
24+
* Docs: Added an Overview section which calls out that whatever conventions apply to Bootstrap's core plugins also apply here
25+
* Docs: Added a callout blurb about the standard attribute validators
26+
* Docs: Added a "Validated fields" section to document the Validator.INPUT_SELECTOR field
27+
* Docs: Removed `$()` from method headers, which was confusing some people ([#202](https://github.com/1000hz/bootstrap-validator/issues/202))
28+
29+
330
### 0.10.2
431
* 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))
532
* 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))

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Cina Saffary
3+
Copyright (c) 2016 Cina Saffary
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ Lastly, don't pollute your patch branch with any unrelated changes.
3333
Thanks to [@mdo](https://github.com/mdo) and [@fat](https://github.com/fat) for [Bootstrap](http://getbootstrap.com). <3
3434

3535
## Copyright and license
36-
Copyright 2015 Cina Saffary under the MIT license.
36+
Copyright 2016 Cina Saffary under the MIT license.

dist/validator.js

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Validator v0.10.2 for Bootstrap 3, by @1000hz
2+
* Validator v0.11.0 for Bootstrap 3, by @1000hz
33
* Copyright 2016 Cina Saffary
44
* Licensed under http://opensource.org/licenses/MIT
55
*
@@ -15,29 +15,20 @@
1515
function getValue($el) {
1616
return $el.is('[type="checkbox"]') ? $el.prop('checked') :
1717
$el.is('[type="radio"]') ? !!$('[name="' + $el.attr('name') + '"]:checked').length :
18-
$.trim($el.val())
18+
$el.val()
1919
}
2020

2121
var Validator = function (element, options) {
22-
this.options = options
23-
this.$element = $(element)
24-
this.$inputs = this.$element.find(Validator.INPUT_SELECTOR)
25-
this.$btn = $('button[type="submit"], input[type="submit"]')
26-
.filter('[form="' + this.$element.attr('id') + '"]')
27-
.add(this.$element.find('input[type="submit"], button[type="submit"]'))
22+
this.options = options
23+
this.validators = $.extend({}, Validator.VALIDATORS, options.custom)
24+
this.$element = $(element)
25+
this.$btn = $('button[type="submit"], input[type="submit"]')
26+
.filter('[form="' + this.$element.attr('id') + '"]')
27+
.add(this.$element.find('input[type="submit"], button[type="submit"]'))
2828

29-
options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors)
29+
this.update()
3030

31-
for (var custom in options.custom) {
32-
if (!options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom)
33-
}
34-
35-
$.extend(Validator.VALIDATORS, options.custom)
36-
37-
this.$element.attr('novalidate', true) // disable automatic native validation
38-
this.toggleSubmit()
39-
40-
this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', Validator.INPUT_SELECTOR, $.proxy(this.onInput, this))
31+
this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.onInput, this))
4132
this.$element.on('submit.bs.validator', $.proxy(this.onSubmit, this))
4233

4334
this.$element.find('[data-match]').each(function () {
@@ -48,9 +39,16 @@
4839
getValue($this) && $this.trigger('input.bs.validator')
4940
})
5041
})
42+
43+
this.$inputs.filter(function () { return getValue($(this)) }).trigger('focusout')
44+
45+
this.$element.attr('novalidate', true) // disable automatic native validation
46+
this.toggleSubmit()
5147
}
5248

53-
Validator.INPUT_SELECTOR = ':input:not([type="submit"], button):enabled:visible'
49+
Validator.VERSION = '0.11.0'
50+
51+
Validator.INPUT_SELECTOR = ':input:not([type="hidden"], [type="submit"], button)'
5452

5553
Validator.FOCUS_OFFSET = 20
5654

@@ -73,36 +71,45 @@
7371
Validator.VALIDATORS = {
7472
'native': function ($el) {
7573
var el = $el[0]
76-
return el.checkValidity ? el.checkValidity() : true
74+
if (el.checkValidity) {
75+
return !el.checkValidity() && !el.validity.valid && (el.validationMessage || "error!")
76+
}
7777
},
7878
'match': function ($el) {
7979
var target = $el.data('match')
80-
return !$el.val() || $el.val() === $(target).val()
80+
return $el.val() !== $(target).val() && Validator.DEFAULTS.errors.match
8181
},
8282
'minlength': function ($el) {
8383
var minlength = $el.data('minlength')
84-
return !$el.val() || $el.val().length >= minlength
84+
return $el.val().length < minlength && Validator.DEFAULTS.errors.minlength
8585
}
8686
}
8787

88+
Validator.prototype.update = function () {
89+
this.$inputs = this.$element.find(Validator.INPUT_SELECTOR)
90+
.add(this.$element.find('[data-validate="true"]'))
91+
.not(this.$element.find('[data-validate="false"]'))
92+
93+
return this
94+
}
95+
8896
Validator.prototype.onInput = function (e) {
8997
var self = this
9098
var $el = $(e.target)
9199
var deferErrors = e.type !== 'focusout'
100+
101+
if (!this.$inputs.is($el)) return
102+
92103
this.validateInput($el, deferErrors).done(function () {
93104
self.toggleSubmit()
94105
})
95106
}
96107

97108
Validator.prototype.validateInput = function ($el, deferErrors) {
98109
var value = getValue($el)
99-
var prevValue = $el.data('bs.validator.previous')
100110
var prevErrors = $el.data('bs.validator.errors')
101111
var errors
102112

103-
if (prevValue === value) return $.Deferred().resolve()
104-
else $el.data('bs.validator.previous', value)
105-
106113
if ($el.is('[type="radio"]')) $el = this.$element.find('input[name="' + $el.attr('name') + '"]')
107114

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

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

144-
function getErrorMessage(key) {
150+
function getValidatorSpecificError(key) {
145151
return $el.data(key + '-error')
146-
|| $el.data('error')
147-
|| key == 'native' && $el[0].validationMessage
148-
|| options.errors[key]
149152
}
150153

151-
$.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
154+
function getValidityStateError() {
155+
var validity = $el[0].validity
156+
return validity.typeMismatch ? $el.data('type-error')
157+
: validity.patternMismatch ? $el.data('pattern-error')
158+
: validity.stepMismatch ? $el.data('step-error')
159+
: validity.rangeOverflow ? $el.data('max-error')
160+
: validity.rangeUnderflow ? $el.data('min-error')
161+
: validity.valueMissing ? $el.data('required-error')
162+
: null
163+
}
164+
165+
function getGenericError() {
166+
return $el.data('error')
167+
}
168+
169+
function getErrorMessage(key) {
170+
return getValidatorSpecificError(key)
171+
|| getValidityStateError()
172+
|| getGenericError()
173+
}
174+
175+
$.each(this.validators, $.proxy(function (key, validator) {
176+
var error = null
152177
if ((getValue($el) || $el.attr('required')) &&
153178
($el.data(key) || key == 'native') &&
154-
!validator.call(this, $el)) {
155-
var error = getErrorMessage(key)
179+
(error = validator.call(this, $el))) {
180+
error = getErrorMessage(key) || error
156181
!~errors.indexOf(error) && errors.push(error)
157182
}
158183
}, this))
@@ -189,7 +214,7 @@
189214
var $input = $(".has-error:first :input")
190215
if ($input.length === 0) return
191216

192-
$(document.body).animate({scrollTop: $input.offset().top - Validator.FOCUS_OFFSET}, 250)
217+
$('html, body').animate({scrollTop: $input.offset().top - Validator.FOCUS_OFFSET}, 250)
193218
$input.focus()
194219
}
195220

@@ -241,7 +266,8 @@
241266

242267
Validator.prototype.isIncomplete = function () {
243268
function fieldIncomplete() {
244-
return !getValue($(this))
269+
var value = getValue($(this))
270+
return !(typeof value == "string" ? $.trim(value) : value)
245271
}
246272

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

275301
this.$inputs
276302
.off('.bs.validator')
277-
.removeData(['bs.validator.errors', 'bs.validator.deferred', 'bs.validator.previous'])
303+
.removeData(['bs.validator.errors', 'bs.validator.deferred'])
278304
.each(function () {
279305
var $this = $(this)
280306
var timeout = $this.data('bs.validator.timeout')

0 commit comments

Comments
 (0)