浏览代码

#434: Add ```updateMessage(field, validator, message)``` method

nghuuphuoc 11 年之前
父节点
当前提交
daa9acf45e
共有 5 个文件被更改,包括 51 次插入14 次删除
  1. 1 0
      CHANGELOG.md
  2. 1 1
      dist/css/bootstrapValidator.min.css
  3. 23 5
      dist/js/bootstrapValidator.js
  4. 4 4
      dist/js/bootstrapValidator.min.js
  5. 22 4
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -24,6 +24,7 @@ __New Features__
 * [#356](https://github.com/nghuuphuoc/bootstrapvalidator/issues/356): Add ```group``` option
 * [#374](https://github.com/nghuuphuoc/bootstrapvalidator/pull/374): Add Singapore postal code to the [zipCode validator](http://bootstrapvalidator.com/validators/zipCode/), thanks to [@thisisclement](https://github.com/thisisclement)
 * [#406](https://github.com/nghuuphuoc/bootstrapvalidator/issues/406): Add ```revalidateField(field)``` method
+* [#434](https://github.com/nghuuphuoc/bootstrapvalidator/issues/434): Add ```updateMessage(field, validator, message)``` method
 
 __Changes__
 * [#42](https://github.com/nghuuphuoc/bootstrapvalidator/issues/42): Remove the submit button from ```submitHandler()```. You can use new ```getSubmitButton()``` method to get the clicked submit button

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

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.0-dev, built on 2014-06-27 8:37:01 AM
+ * @version     v0.5.0-dev, built on 2014-06-27 8:58:08 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 23 - 5
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.0-dev, built on 2014-06-27 8:37:01 AM
+ * @version     v0.5.0-dev, built on 2014-06-27 8:58:08 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -736,8 +736,7 @@
                             // v is validator name
                             $f.removeData('bv.dfs.' + v);
                             if (message) {
-                                // Update the error message
-                                $field.data('bv.messages').find('.help-block[data-bv-validator="' + v + '"][data-bv-for="' + $f.attr('data-bv-field') + '"]').html(message);
+                                that.updateMessage($f, v, message);
                             }
 
                             that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
@@ -1058,9 +1057,11 @@
          *
          * @param {String|jQuery} [field] The field name or field element
          * If the field is not defined, the method returns all error messages of all fields
+         * @param {String} [validator] The name of validator
+         * If the validator is not defined, the method returns error messages of all validators
          * @returns {String[]}
          */
-        getMessages: function(field) {
+        getMessages: function(field, validator) {
             var that     = this,
                 messages = [],
                 $fields  = $([]);
@@ -1081,11 +1082,12 @@
                     break;
             }
 
+            var filter = validator ? '[data-bv-validator="' + validator + '"]' : '';
             $fields.each(function() {
                 messages = messages.concat(
                     $(this)
                         .data('bv.messages')
-                        .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]')
+                        .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]' + filter)
                         .map(function() {
                             return $(this).html();
                         })
@@ -1097,6 +1099,22 @@
         },
 
         /**
+         * Update the error message
+         *
+         * @param {String|jQuery} field The field name or field element
+         * @param {String} validator The validator name
+         * @param {String} message The message
+         * @returns {BootstrapValidator}
+         */
+        updateMessage: function(field, validator, message) {
+            var fields = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                field  = ('object' === typeof field) ? field.attr('data-bv-field')  : field;
+            fields.each(function() {
+                $(this).data('bv.messages').find('.help-block[data-bv-validator="' + validator + '"][data-bv-for="' + field + '"]').html(message);
+            });
+        },
+
+        /**
          * Add a new field
          *
          * @param {String|jQuery} field The field name or field element

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


+ 22 - 4
src/js/bootstrapValidator.js

@@ -735,8 +735,7 @@
                             // v is validator name
                             $f.removeData('bv.dfs.' + v);
                             if (message) {
-                                // Update the error message
-                                $field.data('bv.messages').find('.help-block[data-bv-validator="' + v + '"][data-bv-for="' + $f.attr('data-bv-field') + '"]').html(message);
+                                that.updateMessage($f, v, message);
                             }
 
                             that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
@@ -1057,9 +1056,11 @@
          *
          * @param {String|jQuery} [field] The field name or field element
          * If the field is not defined, the method returns all error messages of all fields
+         * @param {String} [validator] The name of validator
+         * If the validator is not defined, the method returns error messages of all validators
          * @returns {String[]}
          */
-        getMessages: function(field) {
+        getMessages: function(field, validator) {
             var that     = this,
                 messages = [],
                 $fields  = $([]);
@@ -1080,11 +1081,12 @@
                     break;
             }
 
+            var filter = validator ? '[data-bv-validator="' + validator + '"]' : '';
             $fields.each(function() {
                 messages = messages.concat(
                     $(this)
                         .data('bv.messages')
-                        .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]')
+                        .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]' + filter)
                         .map(function() {
                             return $(this).html();
                         })
@@ -1096,6 +1098,22 @@
         },
 
         /**
+         * Update the error message
+         *
+         * @param {String|jQuery} field The field name or field element
+         * @param {String} validator The validator name
+         * @param {String} message The message
+         * @returns {BootstrapValidator}
+         */
+        updateMessage: function(field, validator, message) {
+            var fields = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                field  = ('object' === typeof field) ? field.attr('data-bv-field')  : field;
+            fields.each(function() {
+                $(this).data('bv.messages').find('.help-block[data-bv-validator="' + validator + '"][data-bv-for="' + field + '"]').html(message);
+            });
+        },
+
+        /**
          * Add a new field
          *
          * @param {String|jQuery} field The field name or field element