Browse Source

When parsing options from HTML attributes, don't add the field which hasn't validators (#191, #223)

phuoc 11 years ago
parent
commit
5b04bb72e6

+ 4 - 0
CHANGELOG.md

@@ -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

+ 1 - 1
bootstrapValidator.jquery.json

@@ -1,6 +1,6 @@
 {
     "name": "bootstrapValidator",
-    "version": "0.4.4",
+    "version": "0.4.5-dev",
     "title": "BootstrapValidator",
     "author": {
         "name": "Nguyen Huu Phuoc",

+ 1 - 1
bower.json

@@ -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"

+ 1 - 1
demo/selector.html

@@ -47,7 +47,7 @@
                         <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>
 

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -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

+ 20 - 17
dist/js/bootstrapValidator.js

@@ -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
@@ -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()) + '';
@@ -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];
@@ -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);
@@ -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);
                 }
 

File diff suppressed because it is too large
+ 3 - 3
dist/js/bootstrapValidator.min.js


+ 1 - 1
package.json

@@ -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": {

+ 19 - 16
src/js/bootstrapValidator.js

@@ -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()) + '';
@@ -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];
@@ -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);
@@ -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);
                 }