Browse Source

Improve the removeError() method: Doesn't remove error from field which hasn't been validated

nghuuphuoc 11 years ago
parent
commit
c18f7394b4

+ 10 - 0
demo/remote.html

@@ -95,6 +95,8 @@
 $(document).ready(function() {
     $('#defaultForm').bootstrapValidator({
         message: 'This value is not valid',
+        feedbackIcons: true,
+//        live: 'disabled',
         fields: {
             username: {
                 message: 'The username is not valid',
@@ -105,6 +107,10 @@ $(document).ready(function() {
                     remote: {
                         url: 'remote.php',
                         message: 'The username is not available'
+                    },
+                    different: {
+                        field: 'password',
+                        message: 'The username and password can\'t be the same as each other'
                     }
                 }
             },
@@ -130,6 +136,10 @@ $(document).ready(function() {
                     identical: {
                         field: 'confirmPassword',
                         message: 'The password and its confirm are not the same'
+                    },
+                    different: {
+                        field: 'username',
+                        message: 'The password can\'t be the same as username'
                     }
                 }
             },

+ 8 - 2
dist/js/bootstrapValidator.js

@@ -361,7 +361,8 @@
          */
         removeError: function($field, validatorName) {
             var $parent = $field.parents('.form-group'),
-                $errors = $parent.find('.help-block[data-bs-validator]');
+                $errors = $parent.find('.help-block[data-bs-validator]'),
+                field   = $field.attr('name');
 
             // Hide error element
             $errors
@@ -369,7 +370,12 @@
                     .hide();
 
             // If the field is valid
-            if ($errors.filter(function() { return 'block' == $(this).css('display'); }).length == 0) {
+            var that = this;
+            if ($errors.filter(function() {
+                    var display = $(this).css('display');
+                    return ('block' == display) || ('none' == display && that.results[field][validatorName] == null);
+                }).length == 0
+            ) {
                 $parent
                     .removeClass('has-error')
                     .addClass('has-success')

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


+ 8 - 2
src/js/bootstrapValidator.js

@@ -360,7 +360,8 @@
          */
         removeError: function($field, validatorName) {
             var $parent = $field.parents('.form-group'),
-                $errors = $parent.find('.help-block[data-bs-validator]');
+                $errors = $parent.find('.help-block[data-bs-validator]'),
+                field   = $field.attr('name');
 
             // Hide error element
             $errors
@@ -368,7 +369,12 @@
                     .hide();
 
             // If the field is valid
-            if ($errors.filter(function() { return 'block' == $(this).css('display'); }).length == 0) {
+            var that = this;
+            if ($errors.filter(function() {
+                    var display = $(this).css('display');
+                    return ('block' == display) || ('none' == display && that.results[field][validatorName] == null);
+                }).length == 0
+            ) {
                 $parent
                     .removeClass('has-error')
                     .addClass('has-success')