浏览代码

#20: Add submitHandler option

nghuuphuoc 12 年之前
父节点
当前提交
9f57f7c5fe

+ 1 - 1
demo/index.html

@@ -22,7 +22,7 @@
 
                     <form id="defaultForm" method="post" class="form-horizontal">
                         <div class="form-group">
-                            <label class="col-lg-3 control-label">Username</label>
+                            <label class="col-m col-lg-3 control-label">Username</label>
                             <div class="col-lg-5">
                                 <input type="text" class="form-control" name="username" />
                             </div>

+ 108 - 0
demo/submitHandler.html

@@ -0,0 +1,108 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Submit Handler Demo</title>
+
+    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
+    <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
+
+    <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
+    <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
+    <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
+</head>
+<body>
+<div class="container">
+    <div class="row">
+        <!-- form: -->
+        <section>
+            <div class="col-lg-8 col-lg-offset-2">
+                <div class="page-header">
+                    <h2>Custom submit handler</h2>
+                </div>
+
+                <form id="defaultForm" method="post" class="form-horizontal">
+                    <div class="alert alert-success" style="display: none;"></div>
+
+                    <div class="form-group">
+                        <label class="col-m col-lg-3 control-label">Username</label>
+                        <div class="col-lg-5">
+                            <input type="text" class="form-control" name="username" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-lg-3 control-label">Email address</label>
+                        <div class="col-lg-5">
+                            <input type="text" class="form-control" name="email" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-lg-3 control-label">Password</label>
+                        <div class="col-lg-5">
+                            <input type="password" class="form-control" name="password" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <div class="col-lg-9 col-lg-offset-3">
+                            <button type="submit" class="btn btn-primary">Sign up</button>
+                        </div>
+                    </div>
+                </form>
+            </div>
+        </section>
+        <!-- :form -->
+    </div>
+</div>
+
+<script type="text/javascript">
+    $(document).ready(function() {
+        $('#defaultForm').bootstrapValidator({
+            message: 'This value is not valid',
+            submitHandler: function(validator, form) {
+                // validator is the BootstrapValidator instance
+                // form is the jQuery object present the current form
+                form.find('.alert').html('Thanks for signing up. Now you can sign in as ' + validator.getFieldElement('username').val()).show();
+            },
+            fields: {
+                username: {
+                    message: 'The username is not valid',
+                    validators: {
+                        notEmpty: {
+                            message: 'The username is required and can\'t be empty'
+                        },
+                        stringLength: {
+                            min: 6,
+                            max: 30,
+                            message: 'The username must be more than 6 and less than 30 characters long'
+                        },
+                        regexp: {
+                            regexp: /^[a-zA-Z0-9_\.]+$/,
+                            message: 'The username can only consist of alphabetical, number, dot and underscore'
+                        }
+                    }
+                },
+                email: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The email address is required and can\'t be empty'
+                        },
+                        emailAddress: {
+                            message: 'The input is not a valid email address'
+                        }
+                    }
+                },
+                password: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The password is required and can\'t be empty'
+                        }
+                    }
+                }
+            }
+        });
+    });
+</script>
+</body>
+</html>

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -1,5 +1,5 @@
 /**
- * BootstrapValidator v0.1.1 (http://github.com/nghuuphuoc/bootstrapvalidator)
+ * BootstrapValidator v0.2.0-dev (http://github.com/nghuuphuoc/bootstrapvalidator)
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *

+ 20 - 2
dist/js/bootstrapValidator.js

@@ -1,5 +1,5 @@
 /**
- * BootstrapValidator v0.1.1 (http://github.com/nghuuphuoc/bootstrapvalidator)
+ * BootstrapValidator v0.2.0-dev (http://github.com/nghuuphuoc/bootstrapvalidator)
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *
@@ -33,6 +33,15 @@
         // These buttons will be disabled when the form input are invalid
         submitButtons: 'button[type="submit"]',
 
+        // The custom submit handler
+        // It will prevent the form from the default submitting
+        //
+        //  submitHandler: function(validator, form) {
+        //      - validator is the BootstrapValidator instance
+        //      - form is the jQuery object present the current form
+        //  }
+        submitHandler: null,
+
         // Map the field name with validator rules
         fields: null
     };
@@ -68,6 +77,11 @@
                         if (!that.isValid()) {
                             that.$form.find(that.options.submitButtons).attr('disabled', 'disabled');
                             e.preventDefault();
+                        } else {
+                            if (that.options.submitHandler && 'function' == typeof that.options.submitHandler) {
+                                that.options.submitHandler.call(that, that, that.$form);
+                                return false;
+                            }
                         }
                     }
                 });
@@ -260,7 +274,11 @@
             if (this.numPendingRequests <= 0) {
                 this.numPendingRequests = 0;
                 if (this.formSubmited) {
-                    this.$form.submit();
+                    if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
+                        this.options.submitHandler.call(this, this, this.$form);
+                    } else {
+                        this.$form.submit();
+                    }
                 }
             }
         },

文件差异内容过多而无法显示
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 1 - 1
package.json

@@ -13,5 +13,5 @@
     "grunt-contrib-cssmin": "~0.6.2",
     "grunt-contrib-copy": "~0.4.1"
   },
-  "version": "0.1.1"
+  "version": "0.2.0-dev"
 }

+ 19 - 1
src/js/bootstrapValidator.js

@@ -33,6 +33,15 @@
         // These buttons will be disabled when the form input are invalid
         submitButtons: 'button[type="submit"]',
 
+        // The custom submit handler
+        // It will prevent the form from the default submitting
+        //
+        //  submitHandler: function(validator, form) {
+        //      - validator is the BootstrapValidator instance
+        //      - form is the jQuery object present the current form
+        //  }
+        submitHandler: null,
+
         // Map the field name with validator rules
         fields: null
     };
@@ -68,6 +77,11 @@
                         if (!that.isValid()) {
                             that.$form.find(that.options.submitButtons).attr('disabled', 'disabled');
                             e.preventDefault();
+                        } else {
+                            if (that.options.submitHandler && 'function' == typeof that.options.submitHandler) {
+                                that.options.submitHandler.call(that, that, that.$form);
+                                return false;
+                            }
                         }
                     }
                 });
@@ -260,7 +274,11 @@
             if (this.numPendingRequests <= 0) {
                 this.numPendingRequests = 0;
                 if (this.formSubmited) {
-                    this.$form.submit();
+                    if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
+                        this.options.submitHandler.call(this, this, this.$form);
+                    } else {
+                        this.$form.submit();
+                    }
                 }
             }
         },