Browse Source

#433: Add resetField(field, resetValue) method

nghuuphuoc 11 years ago
parent
commit
6511b307b0

+ 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
+* [#433](https://github.com/nghuuphuoc/bootstrapvalidator/issues/433): Add ```resetField(field, resetValue)``` method
 * [#434](https://github.com/nghuuphuoc/bootstrapvalidator/issues/434): Add ```updateMessage(field, validator, message)``` method
 
 __Changes__

+ 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:58:08 AM
+ * @version     v0.5.0-dev, built on 2014-06-27 9:50:43 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 45 - 30
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:58:08 AM
+ * @version     v0.5.0-dev, built on 2014-06-27 9:50:43 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -758,6 +758,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);
+            });
+        },
+
+        /**
          * Update all validating results of field
          *
          * @param {String|jQuery} field The field name or field element
@@ -1099,22 +1115,6 @@
         },
 
         /**
-         * 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
@@ -1224,30 +1224,45 @@
         },
 
         /**
-         * Reset the form
+         * Reset given field
          *
-         * @param {Boolean} [resetFormData] Reset current form data
+         * @param {String|jQuery} field The field name or field element
+         * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
          * @returns {BootstrapValidator}
          */
-        resetForm: function(resetFormData) {
-            var field, fields, total, type, validator;
-            for (field in this.options.fields) {
-                fields = this.getFieldElements(field);
+        resetField: function(field, resetValue) {
+            var fields = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                field  = ('object' === typeof field) ? field.attr('data-bv-field')  : field,
                 total  = fields.length;
 
+            if (this.options.fields[field]) {
                 for (var i = 0; i < total; i++) {
-                    for (validator in this.options.fields[field].validators) {
+                    for (var validator in this.options.fields[field].validators) {
                         fields.eq(i).removeData('bv.dfs.' + validator);
                     }
                 }
+            }
 
-                // Mark field as not validated yet
-                this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            // Mark field as not validated yet
+            this.updateStatus(field, this.STATUS_NOT_VALIDATED);
 
-                if (resetFormData) {
-                    type = fields.attr('type');
-                    ('radio' === type || 'checkbox' === type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
-                }
+            if (resetValue) {
+                var type = fields.attr('type');
+                ('radio' === type || 'checkbox' === type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
+            }
+
+            return this;
+        },
+
+        /**
+         * Reset the form
+         *
+         * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
+         * @returns {BootstrapValidator}
+         */
+        resetForm: function(resetValue) {
+            for (var field in this.options.fields) {
+                this.resetField(field, resetValue);
             }
 
             this.$invalidFields = $([]);

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


+ 44 - 29
src/js/bootstrapValidator.js

@@ -757,6 +757,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);
+            });
+        },
+
+        /**
          * Update all validating results of field
          *
          * @param {String|jQuery} field The field name or field element
@@ -1098,22 +1114,6 @@
         },
 
         /**
-         * 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
@@ -1223,30 +1223,45 @@
         },
 
         /**
-         * Reset the form
+         * Reset given field
          *
-         * @param {Boolean} [resetFormData] Reset current form data
+         * @param {String|jQuery} field The field name or field element
+         * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
          * @returns {BootstrapValidator}
          */
-        resetForm: function(resetFormData) {
-            var field, fields, total, type, validator;
-            for (field in this.options.fields) {
-                fields = this.getFieldElements(field);
+        resetField: function(field, resetValue) {
+            var fields = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                field  = ('object' === typeof field) ? field.attr('data-bv-field')  : field,
                 total  = fields.length;
 
+            if (this.options.fields[field]) {
                 for (var i = 0; i < total; i++) {
-                    for (validator in this.options.fields[field].validators) {
+                    for (var validator in this.options.fields[field].validators) {
                         fields.eq(i).removeData('bv.dfs.' + validator);
                     }
                 }
+            }
 
-                // Mark field as not validated yet
-                this.updateStatus(field, this.STATUS_NOT_VALIDATED);
+            // Mark field as not validated yet
+            this.updateStatus(field, this.STATUS_NOT_VALIDATED);
 
-                if (resetFormData) {
-                    type = fields.attr('type');
-                    ('radio' === type || 'checkbox' === type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
-                }
+            if (resetValue) {
+                var type = fields.attr('type');
+                ('radio' === type || 'checkbox' === type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
+            }
+
+            return this;
+        },
+
+        /**
+         * Reset the form
+         *
+         * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
+         * @returns {BootstrapValidator}
+         */
+        resetForm: function(resetValue) {
+            for (var field in this.options.fields) {
+                this.resetField(field, resetValue);
             }
 
             this.$invalidFields = $([]);

File diff suppressed because it is too large
+ 532 - 497
test/spec.js


+ 97 - 0
test/spec/message.js

@@ -0,0 +1,97 @@
+describe('message', function() {
+    beforeEach(function() {
+        var html = [
+            '<div class="container">',
+                '<form class="form-horizontal" id="messageForm">',
+                    '<div class="form-group">',
+                        '<input type="password" class="form-control" name="password" placeholder="Enter secure password" />',
+                    '</div>',
+                '</form>',
+            '</div>'
+        ].join('\n');
+
+        $(html).appendTo('body');
+        $('#messageForm').bootstrapValidator({
+            fields: {
+                password: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The password is required'
+                        },
+                        callback: {
+                            callback: function(value, validator) {
+                                // Check the password strength
+                                if (value.length < 6) {
+                                    return {
+                                        valid: false,
+                                        message: 'The password must be more than 6 characters'
+                                    }
+                                }
+
+                                if (value === value.toLowerCase()) {
+                                    return {
+                                        valid: false,
+                                        message: 'The password must contain at least one upper case character'
+                                    }
+                                }
+                                if (value === value.toUpperCase()) {
+                                    return {
+                                        valid: false,
+                                        message: 'The password must contain at least one lower case character'
+                                    }
+                                }
+                                if (value.search(/[0-9]/) < 0) {
+                                    return {
+                                        valid: false,
+                                        message: 'The password must contain at least one digit'
+                                    }
+                                }
+
+                                return true;
+                            }
+                        }
+                    }
+                }
+            }
+        });
+
+        this.bv        = $('#messageForm').data('bootstrapValidator');
+        this.$password = this.bv.getFieldElements('password');
+    });
+
+    afterEach(function() {
+        $('#messageForm').bootstrapValidator('destroy').parent().remove();
+    });
+
+    it('update message from callback', function() {
+        this.bv.resetForm();
+        this.$password.val('123');
+        this.bv.validate();
+        expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must be more than 6 characters');
+
+        this.bv.resetForm();
+        this.$password.val('no_upper_case!@#');
+        this.bv.validate();
+        expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one upper case character');
+
+        this.bv.resetForm();
+        this.$password.val('NO_LOWER_CASE123');
+        this.bv.validate();
+        expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one lower case character');
+
+        this.bv.resetForm();
+        this.$password.val('NoDigits!@#');
+        this.bv.validate();
+        expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one digit');
+    });
+
+    it('call updateMessage()', function() {
+        this.bv.updateStatus('password', this.bv.STATUS_INVALID, 'callback');
+
+        this.bv.updateMessage('password', 'callback', 'The password is weak');
+        expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password is weak');
+
+        this.bv.updateMessage(this.$password, 'callback', 'The password is not strong');
+        expect(this.bv.getMessages(this.$password, 'callback')[0]).toEqual('The password is not strong');
+    });
+});