Browse Source

#324: Add success.validator.bv and error.validator.bv events

phuoc 11 years ago
parent
commit
379dc151fc
4 changed files with 59 additions and 26 deletions
  1. 1 0
      CHANGELOG.md
  2. 28 12
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 28 12
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@
 * [#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
 * Add ```status.field.bv``` event which is triggered after updating the field status. It can be used to solve [#300](https://github.com/nghuuphuoc/bootstrapvalidator/issues/300), [#301](https://github.com/nghuuphuoc/bootstrapvalidator/issues/301)
+* [#324](https://github.com/nghuuphuoc/bootstrapvalidator/issues/324): Add ```success.validator.bv``` and ```error.validator.bv``` events triggered after a validator completes
 * [#164](https://github.com/nghuuphuoc/bootstrapvalidator/issues/164): Add ```container``` option for indicating the element showing all errors
 * [#175](https://github.com/nghuuphuoc/bootstrapvalidator/issues/175): Showing errors in tooltip or popover
 * [#125](https://github.com/nghuuphuoc/bootstrapvalidator/issues/125): Support dynamic fields

+ 28 - 12
dist/js/bootstrapValidator.js

@@ -556,21 +556,41 @@
          * Called after validating a field element
          *
          * @param {jQuery} $field The field element
+         * @param {String} [validatorName] The validator name
          */
-        _onValidateFieldCompleted: function($field) {
+        _onFieldValidated: function($field, validatorName) {
             var field         = $field.attr('data-bv-field'),
                 validators    = this.options.fields[field].validators,
                 counter       = {},
                 numValidators = 0;
 
+            // Trigger an event after given validator completes
+            if (validatorName) {
+                var data = {
+                    field: field,
+                    element: $field,
+                    validator: validatorName
+                };
+                switch ($field.data('bv.result.' + validatorName)) {
+                    case this.STATUS_INVALID:
+                        this.$form.trigger($.Event('error.validator.bv'), data);
+                        break;
+                    case this.STATUS_VALID:
+                        this.$form.trigger($.Event('success.validator.bv'), data);
+                        break;
+                    default:
+                        break;
+                }
+            }
+
             counter[this.STATUS_NOT_VALIDATED] = 0;
             counter[this.STATUS_VALIDATING]    = 0;
             counter[this.STATUS_INVALID]       = 0;
             counter[this.STATUS_VALID]         = 0;
 
-            for (var validatorName in validators) {
+            for (var v in validators) {
                 numValidators++;
-                var result = $field.data('bv.result.' + validatorName);
+                var result = $field.data('bv.result.' + v);
                 if (result) {
                     counter[result]++;
                 }
@@ -658,7 +678,7 @@
         /**
          * Validate given field
          *
-         * @param {String} field The field name
+         * @param {String|jQuery} field The field name or field element
          * @returns {BootstrapValidator}
          */
         validateField: function(field) {
@@ -701,7 +721,7 @@
                     // Don't validate field if it is already done
                     var result = $field.data('bv.result.' + validatorName);
                     if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
-                        this._onValidateFieldCompleted($field);
+                        this._onFieldValidated($field, validatorName);
                         continue;
                     }
 
@@ -866,7 +886,7 @@
                     element: $field,
                     status: status
                 });
-                this._onValidateFieldCompleted($field);
+                this._onFieldValidated($field, validatorName);
             }
 
             return this;
@@ -890,9 +910,7 @@
         /**
          * Check if the field is valid or not
          *
-         * @param {String|jQuery} field Can be
-         * - the field name
-         * - or an jQuery object representing the field
+         * @param {String|jQuery} field The field name or field element
          * @returns {Boolean}
          */
         isValidField: function(field) {
@@ -1000,9 +1018,7 @@
         /**
          * Get the error messages
          *
-         * @param {jQuery|String} [field] The field, which can be
-         * - a string: The field name
-         * - a jQuery object representing the field element
+         * @param {String|jQuery} [field] The field name or field element
          * If the field is not defined, the method returns all error messages of all fields
          * @returns {String[]}
          */

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


+ 28 - 12
src/js/bootstrapValidator.js

@@ -555,21 +555,41 @@
          * Called after validating a field element
          *
          * @param {jQuery} $field The field element
+         * @param {String} [validatorName] The validator name
          */
-        _onValidateFieldCompleted: function($field) {
+        _onFieldValidated: function($field, validatorName) {
             var field         = $field.attr('data-bv-field'),
                 validators    = this.options.fields[field].validators,
                 counter       = {},
                 numValidators = 0;
 
+            // Trigger an event after given validator completes
+            if (validatorName) {
+                var data = {
+                    field: field,
+                    element: $field,
+                    validator: validatorName
+                };
+                switch ($field.data('bv.result.' + validatorName)) {
+                    case this.STATUS_INVALID:
+                        this.$form.trigger($.Event('error.validator.bv'), data);
+                        break;
+                    case this.STATUS_VALID:
+                        this.$form.trigger($.Event('success.validator.bv'), data);
+                        break;
+                    default:
+                        break;
+                }
+            }
+
             counter[this.STATUS_NOT_VALIDATED] = 0;
             counter[this.STATUS_VALIDATING]    = 0;
             counter[this.STATUS_INVALID]       = 0;
             counter[this.STATUS_VALID]         = 0;
 
-            for (var validatorName in validators) {
+            for (var v in validators) {
                 numValidators++;
-                var result = $field.data('bv.result.' + validatorName);
+                var result = $field.data('bv.result.' + v);
                 if (result) {
                     counter[result]++;
                 }
@@ -657,7 +677,7 @@
         /**
          * Validate given field
          *
-         * @param {String} field The field name
+         * @param {String|jQuery} field The field name or field element
          * @returns {BootstrapValidator}
          */
         validateField: function(field) {
@@ -700,7 +720,7 @@
                     // Don't validate field if it is already done
                     var result = $field.data('bv.result.' + validatorName);
                     if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
-                        this._onValidateFieldCompleted($field);
+                        this._onFieldValidated($field, validatorName);
                         continue;
                     }
 
@@ -865,7 +885,7 @@
                     element: $field,
                     status: status
                 });
-                this._onValidateFieldCompleted($field);
+                this._onFieldValidated($field, validatorName);
             }
 
             return this;
@@ -889,9 +909,7 @@
         /**
          * Check if the field is valid or not
          *
-         * @param {String|jQuery} field Can be
-         * - the field name
-         * - or an jQuery object representing the field
+         * @param {String|jQuery} field The field name or field element
          * @returns {Boolean}
          */
         isValidField: function(field) {
@@ -999,9 +1017,7 @@
         /**
          * Get the error messages
          *
-         * @param {jQuery|String} [field] The field, which can be
-         * - a string: The field name
-         * - a jQuery object representing the field element
+         * @param {String|jQuery} [field] The field name or field element
          * If the field is not defined, the method returns all error messages of all fields
          * @returns {String[]}
          */