Browse Source

#184: Add excluded option

nghuuphuoc 11 years ago
parent
commit
9dba0d3956
4 changed files with 71 additions and 10 deletions
  1. 1 0
      CHANGELOG.md
  2. 34 4
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 34 4
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@
 * [#168](https://github.com/nghuuphuoc/bootstrapvalidator/pull/168): Add siren and siret validators, thanks to [@jswale](https://github.com/jswale)
 * [#177](https://github.com/nghuuphuoc/bootstrapvalidator/issues/177): Add Vehicle Identification Number (VIN) validator
 * [#179](https://github.com/nghuuphuoc/bootstrapvalidator/issues/179): Add European VAT number validator
+* [#184](https://github.com/nghuuphuoc/bootstrapvalidator/issues/184): Add ```excluded``` option
 * [#171](https://github.com/nghuuphuoc/bootstrapvalidator/pull/171): The [```phone``` validator](http://bootstrapvalidator.com/validators/phone/) now supports +1 country code and area code for US phone number, thanks to [@tomByrer](https://github.com/tomByrer)
 * [#173](https://github.com/nghuuphuoc/bootstrapvalidator/pull/173): The [```remote``` validator](http://bootstrapvalidator.com/validators/remote/) allows to override ```name``` option, thanks to [@jswale](https://github.com/jswale)
 * [#178](https://github.com/nghuuphuoc/bootstrapvalidator/pull/178): Do not validate fields that ```enabled``` is set to ```false```, thanks to [@henningda](https://github.com/henningda)

+ 34 - 4
dist/js/bootstrapValidator.js

@@ -44,6 +44,14 @@
         // Default invalid message
         message: 'This value is not valid',
 
+        // By default, the plugin will not validate the following kind of fields:
+        // - disabled
+        // - hidden
+        // - invisible
+        excluded: [':disabled', ':hidden', function($field) {
+            return !$field.is(':visible');
+        }],
+
         // Shows ok/error/loading icons based on the field validity.
         // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
         // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
@@ -101,6 +109,7 @@
         _init: function() {
             var that    = this,
                 options = {
+                    excluded:       this.$form.attr('data-bv-excluded') ? this.$form.attr('data-bv-excluded').split(' ') : [],
                     trigger:        this.$form.attr('data-bv-trigger'),
                     message:        this.$form.attr('data-bv-message'),
                     submitButtons:  this.$form.attr('data-bv-submitbuttons'),
@@ -181,7 +190,8 @@
                     }
                 });
 
-            this.options = $.extend(true, this.options, options);
+            this.options          = $.extend(true, this.options, options);
+            this.options.excluded = ('string' == typeof this.options.excluded) ? this.options.excluded.split(' ') : this.options.excluded;
 
             for (var field in this.options.fields) {
                 this._initField(field);
@@ -327,6 +337,27 @@
             }
         },
 
+        /**
+         * Check if the field is excluded.
+         * Returning true means that the field will not be validated
+         *
+         * @param {jQuery} $field The field element
+         * @return {Boolean}
+         */
+        _isExcluded: function($field) {
+            if (this.options.excluded) {
+                for (var i in this.options.excluded) {
+                    if (('string' == typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
+                        || ('function' == typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) == true))
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
+        },
+
         // --- Public methods ---
 
         /**
@@ -453,8 +484,7 @@
                 validatorName,
                 validateResult;
 
-            // We don't need to validate disabled, hidden field
-            if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible') || !this.options.fields[field]['enabled']) {
+            if (!this.options.fields[field]['enabled'] || this._isExcluded($field)) {
                 return this;
             }
 
@@ -622,7 +652,7 @@
 
                 for (i = 0; i < n; i++) {
                     $field = $(fields[i]);
-                    if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
+                    if (this._isExcluded($field)) {
                         continue;
                     }
 

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


+ 34 - 4
src/js/bootstrapValidator.js

@@ -43,6 +43,14 @@
         // Default invalid message
         message: 'This value is not valid',
 
+        // By default, the plugin will not validate the following kind of fields:
+        // - disabled
+        // - hidden
+        // - invisible
+        excluded: [':disabled', ':hidden', function($field) {
+            return !$field.is(':visible');
+        }],
+
         // Shows ok/error/loading icons based on the field validity.
         // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
         // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
@@ -100,6 +108,7 @@
         _init: function() {
             var that    = this,
                 options = {
+                    excluded:       this.$form.attr('data-bv-excluded') ? this.$form.attr('data-bv-excluded').split(' ') : [],
                     trigger:        this.$form.attr('data-bv-trigger'),
                     message:        this.$form.attr('data-bv-message'),
                     submitButtons:  this.$form.attr('data-bv-submitbuttons'),
@@ -180,7 +189,8 @@
                     }
                 });
 
-            this.options = $.extend(true, this.options, options);
+            this.options          = $.extend(true, this.options, options);
+            this.options.excluded = ('string' == typeof this.options.excluded) ? this.options.excluded.split(' ') : this.options.excluded;
 
             for (var field in this.options.fields) {
                 this._initField(field);
@@ -326,6 +336,27 @@
             }
         },
 
+        /**
+         * Check if the field is excluded.
+         * Returning true means that the field will not be validated
+         *
+         * @param {jQuery} $field The field element
+         * @return {Boolean}
+         */
+        _isExcluded: function($field) {
+            if (this.options.excluded) {
+                for (var i in this.options.excluded) {
+                    if (('string' == typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
+                        || ('function' == typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) == true))
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
+        },
+
         // --- Public methods ---
 
         /**
@@ -452,8 +483,7 @@
                 validatorName,
                 validateResult;
 
-            // We don't need to validate disabled, hidden field
-            if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible') || !this.options.fields[field]['enabled']) {
+            if (!this.options.fields[field]['enabled'] || this._isExcluded($field)) {
                 return this;
             }
 
@@ -621,7 +651,7 @@
 
                 for (i = 0; i < n; i++) {
                     $field = $(fields[i]);
-                    if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
+                    if (this._isExcluded($field)) {
                         continue;
                     }