Browse Source

#106: validate() method now doesn't submit the form automatically if user doesn't click the submit buttons

nghuuphuoc 11 years ago
parent
commit
b26ecc29e2
3 changed files with 41 additions and 19 deletions
  1. 21 10
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 19 8
      src/js/bootstrapValidator.js

+ 21 - 10
dist/js/bootstrapValidator.js

@@ -313,14 +313,10 @@
 
             // Call the custom submission if enabled
             if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
-                // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
-                this.$form.off('submit.bv');
+                // If you want to submit the form inside your submit Handler, please call defaultSubmit() method
                 this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
             } else {
-                this.disableSubmitButtons(true);
-
-                // Submit form
-                this.$form.off('submit.bv').submit();
+                this.disableSubmitButtons(true).defaultSubmit();
             }
         },
 
@@ -358,8 +354,8 @@
                             total     = fields.length,
                             updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
                             trigger   = that.options.fields[field].trigger
-                                || that.options.trigger
-                                || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
+                                        || that.options.trigger
+                                        || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
                             events    = $.map(trigger.split(' '), function(item) {
                                 return item + '.live.bv';
                             }).join(' ');
@@ -391,6 +387,7 @@
                 // Don't disable if the live validating mode is disabled
                 this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
             }
+
             return this;
         },
 
@@ -409,7 +406,11 @@
                 this.validateField(field);
             }
 
-            this._submit();
+            // Check if whether the submit button is clicked
+            if (this.$submitButton) {
+                this._submit();
+            }
+
             return this;
         },
 
@@ -475,7 +476,7 @@
                         updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
                                   : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
 
-                        if (isValid && 'disabled' == that.options.live) {
+                        if (isValid && 'disabled' == that.options.live && that.$submitButton) {
                             that._submit();
                         }
                     });
@@ -634,6 +635,16 @@
             return true;
         },
 
+        /**
+         * Submit the form using default submission.
+         * It also does not perform any validations when submitting the form
+         *
+         * It might be used when you want to submit the form right inside the submitHandler()
+         */
+        defaultSubmit: function() {
+            this.$form.off('submit.bv').submit();
+        },
+
         // Useful APIs which aren't used internally
 
         /**

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


+ 19 - 8
src/js/bootstrapValidator.js

@@ -312,14 +312,10 @@
 
             // Call the custom submission if enabled
             if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
-                // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
-                this.$form.off('submit.bv');
+                // If you want to submit the form inside your submit Handler, please call defaultSubmit() method
                 this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
             } else {
-                this.disableSubmitButtons(true);
-
-                // Submit form
-                this.$form.off('submit.bv').submit();
+                this.disableSubmitButtons(true).defaultSubmit();
             }
         },
 
@@ -390,6 +386,7 @@
                 // Don't disable if the live validating mode is disabled
                 this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
             }
+
             return this;
         },
 
@@ -408,7 +405,11 @@
                 this.validateField(field);
             }
 
-            this._submit();
+            // Check if whether the submit button is clicked
+            if (this.$submitButton) {
+                this._submit();
+            }
+
             return this;
         },
 
@@ -474,7 +475,7 @@
                         updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
                                   : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
 
-                        if (isValid && 'disabled' == that.options.live) {
+                        if (isValid && 'disabled' == that.options.live && that.$submitButton) {
                             that._submit();
                         }
                     });
@@ -633,6 +634,16 @@
             return true;
         },
 
+        /**
+         * Submit the form using default submission.
+         * It also does not perform any validations when submitting the form
+         *
+         * It might be used when you want to submit the form right inside the submitHandler()
+         */
+        defaultSubmit: function() {
+            this.$form.off('submit.bv').submit();
+        },
+
         // Useful APIs which aren't used internally
 
         /**