浏览代码

#140: Add invisible example

nghuuphuoc 11 年之前
父节点
当前提交
b5b7598bcc
共有 4 个文件被更改,包括 139 次插入5 次删除
  1. 124 0
      demo/invisible.html
  2. 7 2
      dist/js/bootstrapValidator.js
  3. 1 1
      dist/js/bootstrapValidator.min.js
  4. 7 2
      src/js/bootstrapValidator.js

+ 124 - 0
demo/invisible.html

@@ -0,0 +1,124 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>BootstrapValidator 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>Contact</h2>
+                </div>
+
+                <form id="contactForm" method="post" class="form-horizontal" action="target.php"
+                      data-bv-message="This value is not valid"
+                      data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
+                      data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
+                      data-bv-feedbackicons-validating="glyphicon glyphicon-refresh">
+                    <div class="form-group">
+                        <label class="col-lg-3 control-label">Full name <sup>*</sup></label>
+                        <div class="col-lg-4">
+                            <input type="text" class="form-control" name="firstName" placeholder="First name" required data-bv-notempty-message="The first name is required" />
+                        </div>
+                        <div class="col-lg-4">
+                            <input type="text" class="form-control" name="lastName" placeholder="Last name" required data-bv-notempty-message="The last name is required" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-lg-3 control-label">Company <sup>*</sup></label>
+                        <div class="col-lg-5">
+                            <input type="text" class="form-control" name="company"
+                                   required data-bv-notempty-message="The company name is required" />
+                        </div>
+                        <div class="col-lg-2">
+                            <button type="button" class="btn btn-link" data-toggle="#jobInfo">Add more info</button>
+                        </div>
+                    </div>
+
+                    <div id="jobInfo" style="display: none;">
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Job title <sup>*</sup></label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="job"
+                                       required data-bv-notempty-message="The job title is required" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Department <sup>*</sup></label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="department"
+                                       required data-bv-notempty-message="The department name is required" />
+                            </div>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-lg-3 control-label">Mobile phone <sup>*</sup></label>
+                        <div class="col-lg-5">
+                            <input type="text" class="form-control" name="mobilePhone"
+                                   required data-bv-notempty-message="The mobile phone number is required"
+                                   data-bv-digits data-bv-digits-message="The mobile phone number is not valid" />
+                        </div>
+                        <div class="col-lg-2">
+                            <button type="button" class="btn btn-link" data-toggle="#phoneInfo">Add more phone numbers</button>
+                        </div>
+                    </div>
+
+                    <div id="phoneInfo" style="display: none;">
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Home phone</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="homePhone"
+                                       data-bv-digits data-bv-digits-message="The home phone number is not valid" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Office phone</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="officePhone"
+                                       data-bv-digits data-bv-digits-message="The office phone number is not valid" />
+                            </div>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <div class="col-lg-9 col-lg-offset-3">
+                            <button type="submit" class="btn btn-primary btn-lg">Save</button>
+                        </div>
+                    </div>
+                </form>
+            </div>
+        </section>
+        <!-- :form -->
+    </div>
+</div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('button[data-toggle]').on('click', function() {
+        var $target = $($(this).attr('data-toggle'));
+        $target.toggle();
+        if (!$target.is(':visible')) {
+            // Enable the submit buttons in case additional fields are not valid
+            $('#contactForm').data('bootstrapValidator').disableSubmitButtons(false);
+        }
+    });
+
+    $('#contactForm').bootstrapValidator();
+});
+</script>
+</body>
+</html>

+ 7 - 2
dist/js/bootstrapValidator.js

@@ -125,7 +125,8 @@
                 // Find all fields which have either "name" or "data-bv-field" attribute
                 .find('[name], [data-bv-field]').each(function() {
                     var $field = $(this);
-                    if ($field.is(':hidden') || !$field.is(':visible')) {
+                    // Don't initialize hidden input
+                    if ('hidden' == $field.attr('type')) {
                         return;
                     }
 
@@ -437,7 +438,7 @@
                 validatorName,
                 validateResult;
 
-            // We don't need to validate disabled field
+            // We don't need to validate disabled, hidden field
             if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
                 return this;
             }
@@ -605,6 +606,10 @@
 
                 for (i = 0; i < n; i++) {
                     $field = $(fields[i]);
+                    if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
+                        continue;
+                    }
+
                     for (validatorName in this.options.fields[field].validators) {
                         status = $field.data('bv.result.' + validatorName);
                         if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {

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


+ 7 - 2
src/js/bootstrapValidator.js

@@ -124,7 +124,8 @@
                 // Find all fields which have either "name" or "data-bv-field" attribute
                 .find('[name], [data-bv-field]').each(function() {
                     var $field = $(this);
-                    if ($field.is(':hidden') || !$field.is(':visible')) {
+                    // Don't initialize hidden input
+                    if ('hidden' == $field.attr('type')) {
                         return;
                     }
 
@@ -436,7 +437,7 @@
                 validatorName,
                 validateResult;
 
-            // We don't need to validate disabled field
+            // We don't need to validate disabled, hidden field
             if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
                 return this;
             }
@@ -604,6 +605,10 @@
 
                 for (i = 0; i < n; i++) {
                     $field = $(fields[i]);
+                    if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
+                        continue;
+                    }
+
                     for (validatorName in this.options.fields[field].validators) {
                         status = $field.data('bv.result.' + validatorName);
                         if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {