ソースを参照

#36, #58: Add method to validate form manually

nghuuphuoc 12 年 前
コミット
9073bc9174

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@ __New features__:
 
 * [#44: Rewrite entirely using Deferred](https://github.com/nghuuphuoc/bootstrapvalidator/issues/44)
 * #26, #27, #67: Add choice validator
+* [#36, #58: Add method to validate form manually](https://github.com/nghuuphuoc/bootstrapvalidator/issues/58)
 * [#64: Support Danish zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
 * [#65: Support Sweden zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
 * [#70: Support custom grid columns](https://github.com/nghuuphuoc/bootstrapvalidator/issues/70)

+ 6 - 0
demo/index.html

@@ -157,6 +157,7 @@
                         <div class="form-group">
                             <div class="col-lg-9 col-lg-offset-3">
                                 <button type="submit" class="btn btn-primary">Sign up</button>
+                                <button type="button" class="btn btn-info" id="validateBtn">Manual validate</button>
                             </div>
                         </div>
                     </form>
@@ -276,6 +277,11 @@ $(document).ready(function() {
             }
         }
     });
+
+    // Validate the form manually
+    $('#validateBtn').click(function() {
+        $('#defaultForm').bootstrapValidator('validate');
+    });
 });
 </script>
 </body>

+ 6 - 2
dist/js/bootstrapValidator.js

@@ -230,13 +230,14 @@
          */
         validate: function() {
             if (!this.options.fields) {
-                return;
+                return this;
             }
             for (var field in this.options.fields) {
                 this.validateField(field);
             }
 
             this._submit();
+            return this;
         },
 
         /**
@@ -340,7 +341,10 @@
         return this.each(function() {
             var $this = $(this), data = $this.data('bootstrapValidator');
             if (!data) {
-                $this.data('bootstrapValidator', new BootstrapValidator(this, options));
+                $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
+            }
+            if ('string' == typeof options) {
+                data[options]();
             }
         });
     };

ファイルの差分が大きいため隠しています
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 6 - 2
src/js/bootstrapValidator.js

@@ -229,13 +229,14 @@
          */
         validate: function() {
             if (!this.options.fields) {
-                return;
+                return this;
             }
             for (var field in this.options.fields) {
                 this.validateField(field);
             }
 
             this._submit();
+            return this;
         },
 
         /**
@@ -339,7 +340,10 @@
         return this.each(function() {
             var $this = $(this), data = $this.data('bootstrapValidator');
             if (!data) {
-                $this.data('bootstrapValidator', new BootstrapValidator(this, options));
+                $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
+            }
+            if ('string' == typeof options) {
+                data[options]();
             }
         });
     };