Browse Source

#422: Exclude particular field by ```excluded``` option or ```data-bv-excluded``` attribute

nghuuphuoc 11 years ago
parent
commit
b7767d83c4
4 changed files with 106 additions and 51 deletions
  1. 1 0
      CHANGELOG.md
  2. 51 24
      dist/js/bootstrapValidator.js
  3. 3 3
      dist/js/bootstrapValidator.min.js
  4. 51 24
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -43,6 +43,7 @@ __Improvements__
 * [#382](https://github.com/nghuuphuoc/bootstrapvalidator/issues/382): Add JSHint to Grunt build
 * [#388](https://github.com/nghuuphuoc/bootstrapvalidator/issues/388): Allow to override the default options. Useful for using multiple forms in the same page
 * [#393](https://github.com/nghuuphuoc/bootstrapvalidator/pull/393): The [remote validator](http://bootstrapvalidator.com/validators/remote/) adds support for dynamic ```url``` and method type (GET/POST), thanks to [@ericnakagawa](https://github.com/ericnakagawa)
+* [#422](https://github.com/nghuuphuoc/bootstrapvalidator/issues/422): Exclude particular field by ```excluded``` option or ```data-bv-excluded``` attribute
 
 __Bug Fixes__
 * [#288](https://github.com/nghuuphuoc/bootstrapvalidator/issues/288): Fix [date validator](http://bootstrapvalidator.com/validators/date/) issue on IE8

+ 51 - 24
dist/js/bootstrapValidator.js

@@ -166,6 +166,7 @@
             }
 
             var opts = {
+                    excluded:      $field.attr('data-bv-excluded'),
                     feedbackIcons: $field.attr('data-bv-feedbackicons'),
                     trigger:       $field.attr('data-bv-trigger'),
                     message:       $field.attr('data-bv-message'),
@@ -413,26 +414,41 @@
          * @returns {Boolean}
          */
         _isExcluded: function($field) {
-            if (this.options.excluded) {
-                // Convert to array first
-                if ('string' === typeof this.options.excluded) {
-                    this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
-                        // Trim the spaces
-                        return $.trim(item);
-                    });
-                }
+            var excludedAttr = $field.attr('data-bv-excluded'),
+                // I still need to check the 'name' attribute while initializing the field
+                field        = $field.attr('data-bv-field') || $field.attr('name');
 
-                var length = this.options.excluded.length;
-                for (var i = 0; i < length; i++) {
-                    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;
+            switch (true) {
+                case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'true' || this.options.fields[field].excluded === true)):
+                case (excludedAttr === 'true'):
+                case (excludedAttr === ''):
+                    return true;
+
+                case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'false' || this.options.fields[field].excluded === false)):
+                case (excludedAttr === 'false'):
+                    return false;
+
+                default:
+                    if (this.options.excluded) {
+                        // Convert to array first
+                        if ('string' === typeof this.options.excluded) {
+                            this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
+                                // Trim the spaces
+                                return $.trim(item);
+                            });
+                        }
+
+                        var length = this.options.excluded.length;
+                        for (var i = 0; i < length; i++) {
+                            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;
             }
-
-            return false;
         },
 
         /**
@@ -451,7 +467,9 @@
             return (cannotType || $field.val().length >= threshold);
         },
         
-        // --- Events ---
+        // ---
+        // Events
+        // ---
 
         /**
          * The default handler of error.form.bv event.
@@ -587,7 +605,9 @@
             }
         },
 
-        // --- Public methods ---
+        // ---
+        // Public methods
+        // ---
 
         /**
          * Retrieve the field elements by given name
@@ -754,8 +774,12 @@
                 total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
 
             for (var i = 0; i < total; i++) {
-                var $field       = fields.eq(i),
-                    $parent      = $field.parents(group),
+                var $field       = fields.eq(i);
+                if (this._isExcluded($field)) {
+                    continue;
+                }
+
+                var $parent      = $field.parents(group),
                     $message     = $field.data('bv.messages'),
                     $allErrors   = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
                     $errors      = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
@@ -950,9 +974,10 @@
         isValidContainer: function($container) {
             var that = this, map = {};
             $container.find('[data-bv-field]').each(function() {
-                var field = $(this).attr('data-bv-field');
-                if (!map[field]) {
-                    map[field] = $(this);
+                var $field = $(this),
+                    field  = $field.attr('data-bv-field');
+                if (!that._isExcluded($field) && !map[field]) {
+                    map[field] = $field;
                 }
             });
 
@@ -994,7 +1019,9 @@
             this.$form.off('submit.bv').submit();
         },
 
+        // ---
         // Useful APIs which aren't used internally
+        // ---
 
         /**
          * Get the list of invalid fields

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


+ 51 - 24
src/js/bootstrapValidator.js

@@ -157,6 +157,7 @@
             }
 
             var opts = {
+                    excluded:      $field.attr('data-bv-excluded'),
                     feedbackIcons: $field.attr('data-bv-feedbackicons'),
                     trigger:       $field.attr('data-bv-trigger'),
                     message:       $field.attr('data-bv-message'),
@@ -404,26 +405,41 @@
          * @returns {Boolean}
          */
         _isExcluded: function($field) {
-            if (this.options.excluded) {
-                // Convert to array first
-                if ('string' === typeof this.options.excluded) {
-                    this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
-                        // Trim the spaces
-                        return $.trim(item);
-                    });
-                }
+            var excludedAttr = $field.attr('data-bv-excluded'),
+                // I still need to check the 'name' attribute while initializing the field
+                field        = $field.attr('data-bv-field') || $field.attr('name');
+
+            switch (true) {
+                case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'true' || this.options.fields[field].excluded === true)):
+                case (excludedAttr === 'true'):
+                case (excludedAttr === ''):
+                    return true;
+
+                case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'false' || this.options.fields[field].excluded === false)):
+                case (excludedAttr === 'false'):
+                    return false;
+
+                default:
+                    if (this.options.excluded) {
+                        // Convert to array first
+                        if ('string' === typeof this.options.excluded) {
+                            this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
+                                // Trim the spaces
+                                return $.trim(item);
+                            });
+                        }
 
-                var length = this.options.excluded.length;
-                for (var i = 0; i < length; i++) {
-                    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;
+                        var length = this.options.excluded.length;
+                        for (var i = 0; i < length; i++) {
+                            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;
             }
-
-            return false;
         },
 
         /**
@@ -442,7 +458,9 @@
             return (cannotType || $field.val().length >= threshold);
         },
         
-        // --- Events ---
+        // ---
+        // Events
+        // ---
 
         /**
          * The default handler of error.form.bv event.
@@ -578,7 +596,9 @@
             }
         },
 
-        // --- Public methods ---
+        // ---
+        // Public methods
+        // ---
 
         /**
          * Retrieve the field elements by given name
@@ -745,8 +765,12 @@
                 total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
 
             for (var i = 0; i < total; i++) {
-                var $field       = fields.eq(i),
-                    $parent      = $field.parents(group),
+                var $field       = fields.eq(i);
+                if (this._isExcluded($field)) {
+                    continue;
+                }
+
+                var $parent      = $field.parents(group),
                     $message     = $field.data('bv.messages'),
                     $allErrors   = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
                     $errors      = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
@@ -941,9 +965,10 @@
         isValidContainer: function($container) {
             var that = this, map = {};
             $container.find('[data-bv-field]').each(function() {
-                var field = $(this).attr('data-bv-field');
-                if (!map[field]) {
-                    map[field] = $(this);
+                var $field = $(this),
+                    field  = $field.attr('data-bv-field');
+                if (!that._isExcluded($field) && !map[field]) {
+                    map[field] = $field;
                 }
             });
 
@@ -985,7 +1010,9 @@
             this.$form.off('submit.bv').submit();
         },
 
+        // ---
         // Useful APIs which aren't used internally
+        // ---
 
         /**
          * Get the list of invalid fields