Skip to content

Commit

Permalink
When parsing options from HTML attributes, don't add the field which …
Browse files Browse the repository at this point in the history
…hasn't validators (#191, #223)
  • Loading branch information
nghuuphuoc committed May 6, 2014
1 parent c6abe15 commit 5b04bb7
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.4.5 (not released yet)

* When parsing options from HTML attributes, don't add the field which hasn't validators. It improves fixes for [#191](https://github.com/nghuuphuoc/bootstrapvalidator/issues/191), [#223](https://github.com/nghuuphuoc/bootstrapvalidator/issues/223)

## v0.4.4 (2014-05-05)

* Add ```$.fn.bootstrapValidator.helpers.mod_11_10``` method that implements modulus 11, 10 (ISO 7064) algorithm. The helper is then reused in validating [German and Croatian VAT](http://bootstrapvalidator.com/validators/vat/) numbers
Expand Down
2 changes: 1 addition & 1 deletion bootstrapValidator.jquery.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrapValidator",
"version": "0.4.4",
"version": "0.4.5-dev",
"title": "BootstrapValidator",
"author": {
"name": "Nguyen Huu Phuoc",
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bootstrapValidator",
"description": "The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3",
"version": "0.4.4",
"version": "0.4.5-dev",
"main": [
"dist/css/bootstrapValidator.css",
"dist/js/bootstrapValidator.js"
Expand Down
2 changes: 1 addition & 1 deletion demo/selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>Credit card information</h2>
<div class="form-group">
<label class="col-lg-3 control-label">CVV</label>
<div class="col-lg-2">
<input type="text" class="form-control cvvNumber" />
<input type="text" class="form-control cvvNumber" name="cvv" />
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion dist/css/bootstrapValidator.min.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* A jQuery plugin to validate form fields. Use with Bootstrap 3
*
* @version v0.4.4
* @version v0.4.5
* @author https://twitter.com/nghuuphuoc
* @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
* @license MIT
Expand Down
37 changes: 20 additions & 17 deletions dist/js/bootstrapValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* A jQuery plugin to validate form fields. Use with Bootstrap 3
*
* @version v0.4.4
* @version v0.4.5
* @author https://twitter.com/nghuuphuoc
* @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
* @license MIT
Expand Down Expand Up @@ -170,22 +170,12 @@
// Find all fields which have either "name" or "data-bv-field" attribute
.find('[name], [data-bv-field]').each(function() {
var $field = $(this);
// Don't initialize hidden input
if ('hidden' == $field.attr('type')) {
if (that._isExcluded($field)) {
return;
}

var field = $field.attr('name') || $field.attr('data-bv-field');
$field.attr('data-bv-field', field);

options.fields[field] = $.extend({}, {
trigger: $field.attr('data-bv-trigger'),
message: $field.attr('data-bv-message'),
container: $field.attr('data-bv-container'),
selector: $field.attr('data-bv-selector'),
validators: {}
}, options.fields[field]);

var field = $field.attr('name') || $field.attr('data-bv-field'),
validators = {};
for (v in $.fn.bootstrapValidator.validators) {
validator = $.fn.bootstrapValidator.validators[v];
enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
Expand All @@ -196,7 +186,7 @@
{
// Try to parse the options via attributes
validator.html5Attributes = validator.html5Attributes || { message: 'message' };
options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);

for (html5AttrName in validator.html5Attributes) {
optionName = validator.html5Attributes[html5AttrName];
Expand All @@ -207,11 +197,24 @@
} else if ('false' == optionValue) {
optionValue = false;
}
options.fields[field]['validators'][v][optionName] = optionValue;
validators[v][optionName] = optionValue;
}
}
}
}

// Check if there is any validators set using HTML attributes
if (!$.isEmptyObject(validators)) {
$field.attr('data-bv-field', field);

options.fields[field] = $.extend({}, {
trigger: $field.attr('data-bv-trigger'),
message: $field.attr('data-bv-message'),
container: $field.attr('data-bv-container'),
selector: $field.attr('data-bv-selector'),
validators: validators
}, options.fields[field]);
}
});

this.options = $.extend(true, this.options, options);
Expand Down Expand Up @@ -265,7 +268,7 @@
$message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);

// Set the attribute to indicate the fields which are defined by selector
if (this.options.fields[field].selector) {
if (!$field.attr('data-bv-field')) {
$field.attr('data-bv-field', field);
}

Expand Down
6 changes: 3 additions & 3 deletions dist/js/bootstrapValidator.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrapValidator",
"version": "0.4.4",
"version": "0.4.5-dev",
"description": "The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3",
"keywords": ["jQuery", "plugin", "validate", "validator", "form", "Bootstrap"],
"author": {
Expand Down
35 changes: 19 additions & 16 deletions src/js/bootstrapValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,12 @@
// Find all fields which have either "name" or "data-bv-field" attribute
.find('[name], [data-bv-field]').each(function() {
var $field = $(this);
// Don't initialize hidden input
if ('hidden' == $field.attr('type')) {
if (that._isExcluded($field)) {
return;
}

var field = $field.attr('name') || $field.attr('data-bv-field');
$field.attr('data-bv-field', field);

options.fields[field] = $.extend({}, {
trigger: $field.attr('data-bv-trigger'),
message: $field.attr('data-bv-message'),
container: $field.attr('data-bv-container'),
selector: $field.attr('data-bv-selector'),
validators: {}
}, options.fields[field]);

var field = $field.attr('name') || $field.attr('data-bv-field'),
validators = {};
for (v in $.fn.bootstrapValidator.validators) {
validator = $.fn.bootstrapValidator.validators[v];
enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
Expand All @@ -195,7 +185,7 @@
{
// Try to parse the options via attributes
validator.html5Attributes = validator.html5Attributes || { message: 'message' };
options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);

for (html5AttrName in validator.html5Attributes) {
optionName = validator.html5Attributes[html5AttrName];
Expand All @@ -206,11 +196,24 @@
} else if ('false' == optionValue) {
optionValue = false;
}
options.fields[field]['validators'][v][optionName] = optionValue;
validators[v][optionName] = optionValue;
}
}
}
}

// Check if there is any validators set using HTML attributes
if (!$.isEmptyObject(validators)) {
$field.attr('data-bv-field', field);

options.fields[field] = $.extend({}, {
trigger: $field.attr('data-bv-trigger'),
message: $field.attr('data-bv-message'),
container: $field.attr('data-bv-container'),
selector: $field.attr('data-bv-selector'),
validators: validators
}, options.fields[field]);
}
});

this.options = $.extend(true, this.options, options);
Expand Down Expand Up @@ -264,7 +267,7 @@
$message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);

// Set the attribute to indicate the fields which are defined by selector
if (this.options.fields[field].selector) {
if (!$field.attr('data-bv-field')) {
$field.attr('data-bv-field', field);
}

Expand Down

0 comments on commit 5b04bb7

Please sign in to comment.