ソースを参照

#366: Don't change the enable setting when the new one is the same

nghuuphuoc 11 年 前
コミット
e45eb478e8

+ 1 - 0
CHANGELOG.md

@@ -32,6 +32,7 @@ __Improvements__
 * [#297](https://github.com/nghuuphuoc/bootstrapvalidator/issues/297): Disable feedback icons for particular fields
 * [#348](https://github.com/nghuuphuoc/bootstrapvalidator/issues/348): The [uri validator](http://bootstrapvalidator.com/validators/uri/) now provides an option to support private/local network address
 * [#364](https://github.com/nghuuphuoc/bootstrapvalidator/issues/364): Clicking the feedback icon also effect to the checkbox, radio fields
+* [#366](https://github.com/nghuuphuoc/bootstrapvalidator/issues/366): Don't change the enable setting when the new one is the same
 
 __Bug Fixes__
 * [#288](https://github.com/nghuuphuoc/bootstrapvalidator/issues/288): Fix [```date``` validator](http://bootstrapvalidator.com/validators/date/) issue on IE8

+ 108 - 0
demo/enable2.html

@@ -0,0 +1,108 @@
+<!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">
+            <div class="col-lg-8 col-lg-offset-2">
+                <div class="page-header">
+                    <h2>Enable/disable validator</h2>
+                </div>
+
+                <form id="signupForm" method="post" class="form-horizontal" action="target.php">
+                    <div class="form-group">
+                        <label class="col-md-3 control-label">Full name (*)</label>
+                        <div class="col-md-5">
+                            <input type="text" class="form-control" name="full_name" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-md-3 control-label">Password</label>
+                        <div class="col-md-5">
+                            <input type="password" class="form-control" name="password" />
+                            <span class="help-block">Leave it blank if you don't want to change it</span>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-md-3 control-label">Confirm password</label>
+                        <div class="col-md-5">
+                            <input type="password" class="form-control" name="confirm_password" />
+                            <span class="help-block">Required if the password above is not empty</span>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <div class="col-lg-9 col-lg-offset-3">
+                            <button type="submit" class="btn btn-primary">Submit</button>
+                        </div>
+                    </div>
+                </form>
+            </div>
+        </div>
+    </div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('#signupForm').bootstrapValidator({
+        message: 'This value is not valid',
+        feedbackIcons: {
+            valid: 'glyphicon glyphicon-ok',
+            invalid: 'glyphicon glyphicon-remove',
+            validating: 'glyphicon glyphicon-refresh'
+        },
+        fields: {
+            full_name: {
+                validators: {
+                    notEmpty: {
+                        message: 'The full name is required and cannot be empty'
+                    }
+                }
+            },
+            password: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The password is required and cannot be empty'
+                    },
+                    identical: {
+                        field: 'confirm_password',
+                        message: 'The password and its confirm must be the same'
+                    }
+                }
+            },
+            confirm_password: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The confirm password is required and cannot be empty'
+                    },
+                    identical: {
+                        field: 'password',
+                        message: 'The password and its confirm must be the same'
+                    }
+                }
+            }
+        }
+    });
+
+    // Enable the password/confirm password validators if the password is not empty
+    $('#signupForm').find('[name="password"]').on('keyup', function() {
+        var isEmpty = $(this).val() == '';
+        $('#signupForm').bootstrapValidator('enableFieldValidators', 'password', !isEmpty)
+                        .bootstrapValidator('enableFieldValidators', 'confirm_password', !isEmpty);
+    });
+});
+</script>
+</body>
+</html>

+ 4 - 2
dist/js/bootstrapValidator.js

@@ -1257,8 +1257,10 @@
          * @returns {BootstrapValidator}
          */
         enableFieldValidators: function(field, enabled) {
-            this.options.fields[field]['enabled'] = enabled;
-            this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            if (this.options.fields[field]['enabled'] != enabled) {
+                this.options.fields[field]['enabled'] = enabled;
+                this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            }
 
             return this;
         },

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


+ 4 - 2
src/js/bootstrapValidator.js

@@ -1256,8 +1256,10 @@
          * @returns {BootstrapValidator}
          */
         enableFieldValidators: function(field, enabled) {
-            this.options.fields[field]['enabled'] = enabled;
-            this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            if (this.options.fields[field]['enabled'] != enabled) {
+                this.options.fields[field]['enabled'] = enabled;
+                this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            }
 
             return this;
         },