Browse Source

Merge branch 'mbezhanov-verbose'

Phuoc Nguyen 11 years ago
parent
commit
c6103fbf52
2 changed files with 30 additions and 1 deletions
  1. 15 1
      src/js/bootstrapValidator.js
  2. 15 0
      src/js/validator/allowedValue.js

+ 15 - 1
src/js/bootstrapValidator.js

@@ -814,10 +814,16 @@
                     else if ('object' === typeof validateResult && validateResult.valid !== undefined && validateResult.message !== undefined) {
                         this.updateMessage(updateAll ? field : $field, validatorName, validateResult.message);
                         this.updateStatus(updateAll ? field : $field, validateResult.valid ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
+                        if (!validateResult.valid && !this.options.verbose) {
+                            break;
+                        }
                     }
                     // ... or a boolean value
                     else if ('boolean' === typeof validateResult) {
                         this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
+                        if (!validateResult && !this.options.verbose) {
+                            break;
+                        }
                     }
                 }
             }
@@ -1704,7 +1710,15 @@
             fieldStatus: 'status.field.bv',
             validatorError: 'error.validator.bv',
             validatorSuccess: 'success.validator.bv'
-        }
+        },
+        
+        // Whether to be verbose when validating a field or not.
+        // Possible values:
+        // - true:  when a field has multiple validators, all of them will be checked, and respectively - if errors occur in
+        //          multiple validators, all of them will be displayed to the user
+        // - false: when a field has multiple validators, validation for this field will be terminated upon the first encountered error.
+        //          Thus, only the very first error message related to this field will be displayed to the user
+        verbose: true
     };
 
     // Available validators

+ 15 - 0
src/js/validator/allowedValue.js

@@ -0,0 +1,15 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.allowedValue = {
+        /**
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field The jQuery object represents the field element
+         * @param {Object} options The validator options
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+			var value = $field.val();
+			var allowedValues = (options.allowedValues !== undefined) ? options.allowedValues : [];
+			return ($.inArray(value, allowedValues) > -1 ? true : false);
+        }
+    };
+}(window.jQuery));