Browse Source

#235: Add new method getInvalidFields() that returns all invalid fields

nghuuphuoc 11 years ago
parent
commit
911b251105
5 changed files with 68 additions and 37 deletions
  1. 1 0
      CHANGELOG.md
  2. 12 10
      demo/event.html
  3. 27 13
      dist/js/bootstrapValidator.js
  4. 1 1
      dist/js/bootstrapValidator.min.js
  5. 27 13
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@
 
 
 * [#121](https://github.com/nghuuphuoc/bootstrapvalidator/issues/121): Add events for form validate successfully or not
 * [#121](https://github.com/nghuuphuoc/bootstrapvalidator/issues/121): Add events for form validate successfully or not
 * [#195](https://github.com/nghuuphuoc/bootstrapvalidator/issues/195): Add events for field validation
 * [#195](https://github.com/nghuuphuoc/bootstrapvalidator/issues/195): Add events for field validation
+* [#235](https://github.com/nghuuphuoc/bootstrapvalidator/issues/235): Add new method ```getInvalidFields()``` that returns all invalid fields
 
 
 ## v0.4.5 (not released yet)
 ## v0.4.5 (not released yet)
 
 

+ 12 - 10
demo/event.html

@@ -186,10 +186,6 @@ $(document).ready(function() {
                     validators: {
                     validators: {
                         notEmpty: {
                         notEmpty: {
                             message: 'The first name is required and cannot be empty'
                             message: 'The first name is required and cannot be empty'
-                        },
-                        stringCase: {
-                            message: 'The first name must contain upper case characters only',
-                            case: 'upper'
                         }
                         }
                     }
                     }
                 },
                 },
@@ -288,20 +284,26 @@ $(document).ready(function() {
             }
             }
         })
         })
         .on('error.form.bv', function(e) {
         .on('error.form.bv', function(e) {
-            //console.log('error');
-            // If you want to prevent the default handler (bootstrapValidator._onError)
+            console.log('error');
+
+            // You can get the form instance and then access API
+            var $form = $(e.target);
+            console.log($form.data('bootstrapValidator').getInvalidFields());
+
+            // If you want to prevent the default handler (bootstrapValidator._onError(e))
             // e.preventDefault();
             // e.preventDefault();
         })
         })
         .on('success.form.bv', function(e) {
         .on('success.form.bv', function(e) {
-            //console.log('success');
-            // If you want to prevent the default handler (bootstrapValidator._onSuccess)
+            console.log('success');
+
+            // If you want to prevent the default handler (bootstrapValidator._onSuccess(e))
             // e.preventDefault();
             // e.preventDefault();
         })
         })
         .on('error.field.bv', function(e, field) {
         .on('error.field.bv', function(e, field) {
-            console.log(field, '-->error');
+            //console.log(field, '-->error');
         })
         })
         .on('success.field.bv', function(e, field) {
         .on('success.field.bv', function(e, field) {
-            console.log(field, '-->success');
+            //console.log(field, '-->success');
         });
         });
 
 
     // Validate the form manually
     // Validate the form manually

+ 27 - 13
dist/js/bootstrapValidator.js

@@ -14,8 +14,8 @@
         this.$form   = $(form);
         this.$form   = $(form);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
 
 
-        this.$invalidField = null;  // First invalid field
-        this.$submitButton = null;  // The submit button which is clicked to submit form
+        this.$invalidFields = $([]);    // Array of invalid fields
+        this.$submitButton  = null;     // The submit button which is clicked to submit form
 
 
         // Validating status
         // Validating status
         this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
         this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
@@ -459,15 +459,16 @@
             }
             }
 
 
             // Focus to the first invalid field
             // Focus to the first invalid field
-            if (this.$invalidField) {
+            var $firstInvalidField = this.$invalidFields.eq(0);
+            if ($firstInvalidField) {
                 // Activate the tab containing the invalid field if exists
                 // Activate the tab containing the invalid field if exists
-                var $tab = this.$invalidField.parents('.tab-pane'),
+                var $tab = $firstInvalidField.parents('.tab-pane'),
                     tabId;
                     tabId;
                 if ($tab && (tabId = $tab.attr('id'))) {
                 if ($tab && (tabId = $tab.attr('id'))) {
                     $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
                     $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
                 }
                 }
 
 
-                this.$invalidField.focus();
+                $firstInvalidField.focus();
             }
             }
         },
         },
 
 
@@ -515,11 +516,20 @@
                 }
                 }
             }
             }
 
 
+            var index = this.$invalidFields.index($field);
             if (counter[this.STATUS_VALID] == numValidators) {
             if (counter[this.STATUS_VALID] == numValidators) {
+                // Remove from the list of invalid fields
+                if (index != -1) {
+                    this.$invalidFields.splice(index, 1);
+                }
                 this.$form.trigger($.Event('success.field.bv'), [field, $field]);
                 this.$form.trigger($.Event('success.field.bv'), [field, $field]);
             }
             }
             // If all validators are completed and there is at least one validator which doesn't pass
             // If all validators are completed and there is at least one validator which doesn't pass
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
+                // Add to the list of invalid fields
+                if (index == -1) {
+                    this.$invalidFields = this.$invalidFields.add($field);
+                }
                 this.$form.trigger($.Event('error.field.bv'), [field, $field]);
                 this.$form.trigger($.Event('error.field.bv'), [field, $field]);
             }
             }
         },
         },
@@ -815,12 +825,7 @@
 
 
                     for (validatorName in this.options.fields[field].validators) {
                     for (validatorName in this.options.fields[field].validators) {
                         status = $field.data('bv.result.' + validatorName);
                         status = $field.data('bv.result.' + validatorName);
-                        if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
-                            return false;
-                        }
-
-                        if (status == this.STATUS_INVALID) {
-                            this.$invalidField = $field;
+                        if (status != this.STATUS_VALID) {
                             return false;
                             return false;
                         }
                         }
                     }
                     }
@@ -843,6 +848,15 @@
         // Useful APIs which aren't used internally
         // Useful APIs which aren't used internally
 
 
         /**
         /**
+         * Get the list of invalid fields
+         *
+         * @returns {jQuery[]}
+         */
+        getInvalidFields: function() {
+            return this.$invalidFields;
+        },
+
+        /**
          * Add new field element
          * Add new field element
          *
          *
          * @param {jQuery} $field The field element
          * @param {jQuery} $field The field element
@@ -915,8 +929,8 @@
                 }
                 }
             }
             }
 
 
-            this.$invalidField = null;
-            this.$submitButton = null;
+            this.$invalidFields = $([]);
+            this.$submitButton  = null;
 
 
             // Enable submit buttons
             // Enable submit buttons
             this.disableSubmitButtons(false);
             this.disableSubmitButtons(false);

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


+ 27 - 13
src/js/bootstrapValidator.js

@@ -13,8 +13,8 @@
         this.$form   = $(form);
         this.$form   = $(form);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
 
 
-        this.$invalidField = null;  // First invalid field
-        this.$submitButton = null;  // The submit button which is clicked to submit form
+        this.$invalidFields = $([]);    // Array of invalid fields
+        this.$submitButton  = null;     // The submit button which is clicked to submit form
 
 
         // Validating status
         // Validating status
         this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
         this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
@@ -458,15 +458,16 @@
             }
             }
 
 
             // Focus to the first invalid field
             // Focus to the first invalid field
-            if (this.$invalidField) {
+            var $firstInvalidField = this.$invalidFields.eq(0);
+            if ($firstInvalidField) {
                 // Activate the tab containing the invalid field if exists
                 // Activate the tab containing the invalid field if exists
-                var $tab = this.$invalidField.parents('.tab-pane'),
+                var $tab = $firstInvalidField.parents('.tab-pane'),
                     tabId;
                     tabId;
                 if ($tab && (tabId = $tab.attr('id'))) {
                 if ($tab && (tabId = $tab.attr('id'))) {
                     $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
                     $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
                 }
                 }
 
 
-                this.$invalidField.focus();
+                $firstInvalidField.focus();
             }
             }
         },
         },
 
 
@@ -514,11 +515,20 @@
                 }
                 }
             }
             }
 
 
+            var index = this.$invalidFields.index($field);
             if (counter[this.STATUS_VALID] == numValidators) {
             if (counter[this.STATUS_VALID] == numValidators) {
+                // Remove from the list of invalid fields
+                if (index != -1) {
+                    this.$invalidFields.splice(index, 1);
+                }
                 this.$form.trigger($.Event('success.field.bv'), [field, $field]);
                 this.$form.trigger($.Event('success.field.bv'), [field, $field]);
             }
             }
             // If all validators are completed and there is at least one validator which doesn't pass
             // If all validators are completed and there is at least one validator which doesn't pass
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
+                // Add to the list of invalid fields
+                if (index == -1) {
+                    this.$invalidFields = this.$invalidFields.add($field);
+                }
                 this.$form.trigger($.Event('error.field.bv'), [field, $field]);
                 this.$form.trigger($.Event('error.field.bv'), [field, $field]);
             }
             }
         },
         },
@@ -814,12 +824,7 @@
 
 
                     for (validatorName in this.options.fields[field].validators) {
                     for (validatorName in this.options.fields[field].validators) {
                         status = $field.data('bv.result.' + validatorName);
                         status = $field.data('bv.result.' + validatorName);
-                        if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
-                            return false;
-                        }
-
-                        if (status == this.STATUS_INVALID) {
-                            this.$invalidField = $field;
+                        if (status != this.STATUS_VALID) {
                             return false;
                             return false;
                         }
                         }
                     }
                     }
@@ -842,6 +847,15 @@
         // Useful APIs which aren't used internally
         // Useful APIs which aren't used internally
 
 
         /**
         /**
+         * Get the list of invalid fields
+         *
+         * @returns {jQuery[]}
+         */
+        getInvalidFields: function() {
+            return this.$invalidFields;
+        },
+
+        /**
          * Add new field element
          * Add new field element
          *
          *
          * @param {jQuery} $field The field element
          * @param {jQuery} $field The field element
@@ -914,8 +928,8 @@
                 }
                 }
             }
             }
 
 
-            this.$invalidField = null;
-            this.$submitButton = null;
+            this.$invalidFields = $([]);
+            this.$submitButton  = null;
 
 
             // Enable submit buttons
             // Enable submit buttons
             this.disableSubmitButtons(false);
             this.disableSubmitButtons(false);