Browse Source

#1055, #1057, #1063: Fix issues caused by set verbose to false, thanks to @shineability

phuoc 11 years ago
parent
commit
bd15caebc1

+ 2 - 0
CHANGELOG.md

@@ -27,6 +27,8 @@ __Bug Fixes__
 * [#1014](https://github.com/nghuuphuoc/bootstrapvalidator/pull/1014): Fix [isValidField()](http://bootstrapvalidator.com/api/#is-valid-field) and [validateField()](http://bootstrapvalidator.com/api/#validate-field) methods for fields without validators, thanks to [@jazzzz](https://github.com/jazzzz)
 * [#1040](https://github.com/nghuuphuoc/bootstrapvalidator/issues/1040), [#1041](https://github.com/nghuuphuoc/bootstrapvalidator/pull/1041): Fix the issue where input with placeholder is auto validated on IE 10, 11, thanks to [@jazzzz](https://github.com/jazzzz)
 * [#1050](https://github.com/nghuuphuoc/bootstrapvalidator/issues/1050): Fix the issue when using multiple fields with same name, the tooltip of the last element is always shown
+* [#1055](https://github.com/nghuuphuoc/bootstrapvalidator/issues/1055), [#1063](https://github.com/nghuuphuoc/bootstrapvalidator/pull/1063): The [error.field.bv](http://bootstrapvalidator.com/settings/#event-field) event isn't triggered if verbose is set to false, thanks to [@shineability](https://github.com/shineability)
+* [#1057](https://github.com/nghuuphuoc/bootstrapvalidator/issues/1057), [#1063](https://github.com/nghuuphuoc/bootstrapvalidator/pull/1063): The [verbose](http://bootstrapvalidator.com/settings/#field-verbose) option for field doesn't override the form level, thanks to [@shineability](https://github.com/shineability)
 
 __Document__
 * [#848](https://github.com/nghuuphuoc/bootstrapvalidator/pull/848): Update the [stringLength](http://bootstrapvalidator.com/validators/stringLength) document, thanks to [@Relequestual](https://github.com/Relequestual)

+ 1 - 0
CONTRIBUTORS.md

@@ -87,6 +87,7 @@ I would like to give big thanks to the following contributors:
 * [@rtbfreitas](https://github.com/rtbfreitas)
 * [@ryan2049](https://github.com/ryan2049)
 * [@shamiao](https://github.com/shamiao)
+* [@shineability](https://github.com/shineability)
 * [@smeagol74](https://github.com/smeagol74)
 * [@stephengreentree](https://github.com/stephengreentree)
 * [@stepin](https://github.com/stepin)

+ 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.3-dev, built on 2014-11-02 4:33:10 PM
+ * @version     v0.5.3-dev, built on 2014-11-03 12:34:21 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 20 - 3
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.3-dev, built on 2014-11-02 4:33:10 PM
+ * @version     v0.5.3-dev, built on 2014-11-03 12:34:21 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -735,7 +735,7 @@ if (typeof jQuery === 'undefined') {
                 $field.trigger($.Event(this.options.events.fieldSuccess), data);
             }
             // If all validators are completed and there is at least one validator which doesn't pass
-            else if (counter[this.STATUS_NOT_VALIDATED] === 0 && counter[this.STATUS_VALIDATING] === 0 && counter[this.STATUS_INVALID] > 0) {
+            else if ((counter[this.STATUS_NOT_VALIDATED] === 0 || !this._isVerboseField(field)) && counter[this.STATUS_VALIDATING] === 0 && counter[this.STATUS_INVALID] > 0) {
                 // Add to the list of invalid fields
                 this.$invalidFields = this.$invalidFields.add($field);
 
@@ -743,6 +743,23 @@ if (typeof jQuery === 'undefined') {
             }
         },
 
+        /**
+         * Check whether or not a field is verbose
+         *
+         * @param {String} field The field name
+         * @returns {Boolean}
+         */
+        _isVerboseField: function(field)
+        {
+            if (this.options.fields[field].verbose === 'true' || this.options.fields[field].verbose === true) {
+                return true;
+            }
+            if (this.options.fields[field].verbose === 'false' || this.options.fields[field].verbose === false) {
+                return false;
+            }
+            return this.options.verbose === 'true' || this.options.verbose === true;
+        },
+
         // ---
         // Public methods
         // ---
@@ -860,7 +877,7 @@ if (typeof jQuery === 'undefined') {
                 total      = ('radio' === type || 'checkbox' === type) ? 1 : fields.length,
                 updateAll  = ('radio' === type || 'checkbox' === type),
                 validators = this.options.fields[field].validators,
-                verbose    = this.options.fields[field].verbose === 'true' || this.options.fields[field].verbose === true || this.options.verbose === 'true' || this.options.verbose === true,
+                verbose    = this._isVerboseField(field),
                 validatorName,
                 validateResult;
 

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


+ 8 - 10
src/js/bootstrapValidator.js

@@ -743,22 +743,20 @@ if (typeof jQuery === 'undefined') {
             }
         },
 
+        /**
+         * Check whether or not a field is verbose
+         *
+         * @param {String} field The field name
+         * @returns {Boolean}
+         */
         _isVerboseField: function(field)
         {
-            if (this.options.fields[field].verbose === 'true' || this.options.fields[field].verbose === true)
-            {
+            if (this.options.fields[field].verbose === 'true' || this.options.fields[field].verbose === true) {
                 return true;
             }
-            else if (this.options.fields[field].verbose === 'false' || this.options.fields[field].verbose === false)
-            {
+            if (this.options.fields[field].verbose === 'false' || this.options.fields[field].verbose === false) {
                 return false;
             }
-
-            return this._isVerbose();
-        },
-
-        _isVerbose: function()
-        {
             return this.options.verbose === 'true' || this.options.verbose === true;
         },
 

+ 65 - 8
test/spec.js

@@ -1024,6 +1024,8 @@ describe('event field trigger with default events', function() {
 });
 
 describe('event form trigger with events changed', function() {
+    var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
+
     beforeEach(function() {
         $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
             events: {
@@ -1070,6 +1072,7 @@ describe('event form trigger with events changed', function() {
     });
 
     afterEach(function() {
+        $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
         $('#eventForm2').bootstrapValidator('destroy').remove();
     });
 
@@ -1097,7 +1100,9 @@ describe('event form trigger with events changed', function() {
 });
 
 describe('event field trigger with events changed', function() {
-    beforeEach(function () {
+    var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
+
+    beforeEach(function() {
         $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
             events: {
                 formInit: 'init.form.bv',
@@ -1143,6 +1148,7 @@ describe('event field trigger with events changed', function() {
     });
 
     afterEach(function() {
+        $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
         $('#eventForm4').bootstrapValidator('destroy').remove();
     });
 
@@ -2014,7 +2020,11 @@ describe('verbose option', function() {
     });
 
     it('set data-bv-verbose="false" for form', function() {
-        var bv        = $('#verboseForm').attr('data-bv-verbose', 'false').bootstrapValidator().data('bootstrapValidator'),
+        var bv        = $('#verboseForm')
+                            .attr('data-bv-verbose', 'false')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator()
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -2025,7 +2035,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -2045,7 +2055,9 @@ describe('verbose option', function() {
                             .find('[name="fullName"]')
                                 .attr('data-bv-verbose', 'false')
                                 .end()
-                            .bootstrapValidator().data('bootstrapValidator'),
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator()
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -2056,7 +2068,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -2071,7 +2083,10 @@ describe('verbose option', function() {
     });
 
     it('set verbose: "false" for form', function() {
-        var bv        = $('#verboseForm').bootstrapValidator({ verbose: false }).data('bootstrapValidator'),
+        var bv        = $('#verboseForm')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator({ verbose: false })
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -2082,7 +2097,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -2096,9 +2111,11 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
     });
 
+    // #1057
     it('set verbose: "false" for field', function() {
         var bv        = $('#verboseForm')
                             .attr('data-bv-verbose', 'true')
+                            .bootstrapValidator('destroy')
                             .bootstrapValidator({
                                 verbose: true,
                                 fields: {
@@ -2118,7 +2135,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -2131,6 +2148,46 @@ describe('verbose option', function() {
         expect(messages.length).toEqual(1);
         expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
     });
+
+    // #1055
+    it('trigger "error.field.bv" event', function() {
+        var validators = [],    // Array of not passed validators
+            bv         = $('#verboseForm')
+                            .attr('data-bv-verbose', 'true')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator({
+                                verbose: true,
+                                fields: {
+                                    fullName: {
+                                        verbose: false
+                                    }
+                                }
+                            })
+                            .on('error.field.bv', function(e, data) {
+                                validators.push(data.validator);
+                            })
+                            .data('bootstrapValidator'),
+            $fullName  = bv.getFieldElements('fullName');
+
+        $fullName.val('');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('notEmpty');
+
+        validators = [];
+        bv.resetForm();
+        $fullName.val('Spe@#$');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('regexp');
+
+        validators = [];
+        bv.resetForm();
+        $fullName.val('Full');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('stringLength');
+    });
 });
 
 function betweenCompareMin() {

+ 7 - 1
test/spec/event.js

@@ -483,6 +483,8 @@ describe('event field trigger with default events', function() {
 });
 
 describe('event form trigger with events changed', function() {
+    var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
+
     beforeEach(function() {
         $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
             events: {
@@ -529,6 +531,7 @@ describe('event form trigger with events changed', function() {
     });
 
     afterEach(function() {
+        $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
         $('#eventForm2').bootstrapValidator('destroy').remove();
     });
 
@@ -556,7 +559,9 @@ describe('event form trigger with events changed', function() {
 });
 
 describe('event field trigger with events changed', function() {
-    beforeEach(function () {
+    var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
+
+    beforeEach(function() {
         $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
             events: {
                 formInit: 'init.form.bv',
@@ -602,6 +607,7 @@ describe('event field trigger with events changed', function() {
     });
 
     afterEach(function() {
+        $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
         $('#eventForm4').bootstrapValidator('destroy').remove();
     });
 

+ 58 - 7
test/spec/verbose.js

@@ -23,7 +23,11 @@ describe('verbose option', function() {
     });
 
     it('set data-bv-verbose="false" for form', function() {
-        var bv        = $('#verboseForm').attr('data-bv-verbose', 'false').bootstrapValidator().data('bootstrapValidator'),
+        var bv        = $('#verboseForm')
+                            .attr('data-bv-verbose', 'false')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator()
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -34,7 +38,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -54,7 +58,9 @@ describe('verbose option', function() {
                             .find('[name="fullName"]')
                                 .attr('data-bv-verbose', 'false')
                                 .end()
-                            .bootstrapValidator().data('bootstrapValidator'),
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator()
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -65,7 +71,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -80,7 +86,10 @@ describe('verbose option', function() {
     });
 
     it('set verbose: "false" for form', function() {
-        var bv        = $('#verboseForm').bootstrapValidator({ verbose: false }).data('bootstrapValidator'),
+        var bv        = $('#verboseForm')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator({ verbose: false })
+                            .data('bootstrapValidator'),
             $fullName = bv.getFieldElements('fullName'),
             messages;
 
@@ -91,7 +100,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -105,9 +114,11 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
     });
 
+    // #1057
     it('set verbose: "false" for field', function() {
         var bv        = $('#verboseForm')
                             .attr('data-bv-verbose', 'true')
+                            .bootstrapValidator('destroy')
                             .bootstrapValidator({
                                 verbose: true,
                                 fields: {
@@ -127,7 +138,7 @@ describe('verbose option', function() {
         expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
 
         bv.resetForm();
-        $fullName.val('Special@#$');
+        $fullName.val('Spe@#$');
         bv.validate();
         messages = bv.getMessages('fullName');
         expect(messages.length).toEqual(1);
@@ -140,4 +151,44 @@ describe('verbose option', function() {
         expect(messages.length).toEqual(1);
         expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
     });
+
+    // #1055
+    it('trigger "error.field.bv" event', function() {
+        var validators = [],    // Array of not passed validators
+            bv         = $('#verboseForm')
+                            .attr('data-bv-verbose', 'true')
+                            .bootstrapValidator('destroy')
+                            .bootstrapValidator({
+                                verbose: true,
+                                fields: {
+                                    fullName: {
+                                        verbose: false
+                                    }
+                                }
+                            })
+                            .on('error.field.bv', function(e, data) {
+                                validators.push(data.validator);
+                            })
+                            .data('bootstrapValidator'),
+            $fullName  = bv.getFieldElements('fullName');
+
+        $fullName.val('');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('notEmpty');
+
+        validators = [];
+        bv.resetForm();
+        $fullName.val('Spe@#$');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('regexp');
+
+        validators = [];
+        bv.resetForm();
+        $fullName.val('Full');
+        bv.validate();
+        expect(validators.length).toEqual(1);
+        expect(validators[0]).toEqual('stringLength');
+    });
 });