Browse Source

update submit form after ajax success + check element is validated

khangvm53 12 years ago
parent
commit
6228822728

+ 22 - 5
dist/js/bootstrapValidator.js

@@ -48,10 +48,15 @@
                     if (that.options.fields) {
                         for (var field in that.options.fields) {
                             if(that.numPendingRequests > 0 || that.numPendingRequests == null ){
-                                that.validateField(field);
+                                // Check that field is validated?
+                                var $field = that.getFieldElement(field);
+                                if( $field.attr('fieldIsValid') == 1 || $field.attr('fieldIsValid') == '1' ){
+                                }else{
+                                    that.validateField(field);
+                                }
                             }
                         }
-                        if (!that.isValid() || that.numPendingRequests == null) {
+                        if (!that.isValid()) {
                             e.preventDefault();
                         }
                     }
@@ -164,6 +169,9 @@
 
         startRequest: function($field, validatorName, xhr) {
             var field = $field.attr('name');
+
+            //when request start, I assigned arbitrary attribute fieldIsValid = 0
+            $field.attr('fieldIsValid',0);
             if(this.numPendingRequests == null){
                 this.numPendingRequests = 0;
             }
@@ -185,19 +193,26 @@
                 this.showError($field, validatorName);
             } else if (isValid === true ) {
                 this.removeError($field);
+
+                /* When request complete and that field is validated,
+                  I assigned arbitrary attribute fieldIsValid = 1
+                */
+                $field.attr('fieldIsValid', 1);
             }
 
             var field = $field.attr('name');
 
+            delete this.xhrRequests[field][validatorName];
+
             this.numPendingRequests--;
-            if (this.numPendingRequests < 0) {
+            if (this.numPendingRequests <= 0) {
                 this.numPendingRequests = 0;
+                this.$form.submit();
             }
-            delete this.xhrRequests[field][validatorName];
         },
 
         isValid: function() {
-            if (this.numPendingRequests > 0) {
+            if (this.numPendingRequests > 0 || this.numPendingRequests == null ) {
                 return false;
             }
             for (var field in this.invalidFields) {
@@ -422,8 +437,10 @@
                 data: data
             }).success(function(response) {
                 var isValid =  response.valid === true || response.valid === 'true';
+
                 validator.completeRequest($field, 'remote', isValid);
             });
+
             validator.startRequest($field, 'remote', xhr);
 
             return 'pending';

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


+ 20 - 5
src/js/bootstrapValidator.js

@@ -48,10 +48,15 @@
                     if (that.options.fields) {
                         for (var field in that.options.fields) {
                             if(that.numPendingRequests > 0 || that.numPendingRequests == null ){
-                                that.validateField(field);
+                                // Check that field is validated?
+                                var $field = that.getFieldElement(field);
+                                if( $field.attr('fieldIsValid') == 1 || $field.attr('fieldIsValid') == '1' ){
+                                }else{
+                                    that.validateField(field);
+                                }
                             }
                         }
-                        if (!that.isValid() || that.numPendingRequests == null) {
+                        if (!that.isValid()) {
                             e.preventDefault();
                         }
                     }
@@ -164,6 +169,9 @@
 
         startRequest: function($field, validatorName, xhr) {
             var field = $field.attr('name');
+
+            //when request start, I assigned arbitrary attribute fieldIsValid = 0
+            $field.attr('fieldIsValid',0);
             if(this.numPendingRequests == null){
                 this.numPendingRequests = 0;
             }
@@ -185,19 +193,26 @@
                 this.showError($field, validatorName);
             } else if (isValid === true ) {
                 this.removeError($field);
+
+                /* When request complete and that field is validated,
+                  I assigned arbitrary attribute fieldIsValid = 1
+                */
+                $field.attr('fieldIsValid', 1);
             }
 
             var field = $field.attr('name');
 
+            delete this.xhrRequests[field][validatorName];
+
             this.numPendingRequests--;
-            if (this.numPendingRequests < 0) {
+            if (this.numPendingRequests <= 0) {
                 this.numPendingRequests = 0;
+                this.$form.submit();
             }
-            delete this.xhrRequests[field][validatorName];
         },
 
         isValid: function() {
-            if (this.numPendingRequests > 0) {
+            if (this.numPendingRequests > 0 || this.numPendingRequests == null ) {
                 return false;
             }
             for (var field in this.invalidFields) {

+ 2 - 0
src/js/validator/remote.js

@@ -29,8 +29,10 @@
                 data: data
             }).success(function(response) {
                 var isValid =  response.valid === true || response.valid === 'true';
+
                 validator.completeRequest($field, 'remote', isValid);
             });
+
             validator.startRequest($field, 'remote', xhr);
 
             return 'pending';