Browse Source

#465: Refactoring stringLength validator

phuoc 11 years ago
parent
commit
6029c3833c

+ 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-07-06 8:37:51 AM
+ * @version     v0.5.0-dev, built on 2014-07-06 9:00:01 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 36 - 21
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-07-06 8:37:51 AM
+ * @version     v0.5.0-dev, built on 2014-07-06 9:00:01 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -2870,7 +2870,7 @@
          *      - Name of field which its value defines the country code
          *      - Name of callback function that returns the country code
          *      - A callback function that returns the country code
-         * @returns {Boolean|Object}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -4721,20 +4721,7 @@
         'default': 'Please enter a value with valid length',
         less: 'Please enter less than %s characters',
         more: 'Please enter more than %s characters',
-        between: 'Please enter value between %s and %s characters long',
-
-        getMessage: function(options) {
-            switch (true) {
-                case (!!options.min && !!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.between, [options.min, options.max]);
-                case (!!options.min):
-                    return $.fn.bootstrapValidator.helpers.format(this.more, options.min);
-                case (!!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.less, options.max);
-                default:
-                    return this['default'];
-            }
-        }
+        between: 'Please enter value between %s and %s characters long'
     });
 
     $.fn.bootstrapValidator.validators.stringLength = {
@@ -4764,8 +4751,14 @@
          * - min
          * - max
          * At least one of two keys is required
+         * The min, max keys define the number which the field value compares to. min, max can be
+         *      - A number
+         *      - Name of field which its value defines the number
+         *      - Name of callback function that returns the number
+         *      - A callback function that returns the number
+         *
          * - message: The invalid message
-         * @returns {Boolean}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -4773,12 +4766,34 @@
                 return true;
             }
 
-            var length = value.length;
-            if ((options.min && length < options.min) || (options.max && length > options.max)) {
-                return false;
+            var min     = $.isNumeric(options.min) ? options.min : validator.getDynamicOption(options.min, $field),
+                max     = $.isNumeric(options.max) ? options.max : validator.getDynamicOption(options.max, $field),
+                length  = value.length,
+                isValid = true,
+                message = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];
+
+            if ((min && length < parseInt(min, 10)) || (max && length > parseInt(max, 10))) {
+                isValid = false;
             }
 
-            return true;
+            switch (true) {
+                case (!!min && !!max):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.between, [parseInt(min, 10), parseInt(max, 10)]);
+                    break;
+
+                case (!!min):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.more, parseInt(min, 10));
+                    break;
+
+                case (!!max):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.less, parseInt(max, 10));
+                    break;
+
+                default:
+                    break;
+            }
+
+            return { valid: isValid, message: message };
         }
     };
 }(window.jQuery));

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


+ 1 - 1
src/js/validator/iban.js

@@ -190,7 +190,7 @@
          *      - Name of field which its value defines the country code
          *      - Name of callback function that returns the country code
          *      - A callback function that returns the country code
-         * @returns {Boolean|Object}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();

+ 34 - 19
src/js/validator/stringLength.js

@@ -3,20 +3,7 @@
         'default': 'Please enter a value with valid length',
         less: 'Please enter less than %s characters',
         more: 'Please enter more than %s characters',
-        between: 'Please enter value between %s and %s characters long',
-
-        getMessage: function(options) {
-            switch (true) {
-                case (!!options.min && !!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.between, [options.min, options.max]);
-                case (!!options.min):
-                    return $.fn.bootstrapValidator.helpers.format(this.more, options.min);
-                case (!!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.less, options.max);
-                default:
-                    return this['default'];
-            }
-        }
+        between: 'Please enter value between %s and %s characters long'
     });
 
     $.fn.bootstrapValidator.validators.stringLength = {
@@ -46,8 +33,14 @@
          * - min
          * - max
          * At least one of two keys is required
+         * The min, max keys define the number which the field value compares to. min, max can be
+         *      - A number
+         *      - Name of field which its value defines the number
+         *      - Name of callback function that returns the number
+         *      - A callback function that returns the number
+         *
          * - message: The invalid message
-         * @returns {Boolean}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -55,12 +48,34 @@
                 return true;
             }
 
-            var length = value.length;
-            if ((options.min && length < options.min) || (options.max && length > options.max)) {
-                return false;
+            var min     = $.isNumeric(options.min) ? options.min : validator.getDynamicOption(options.min, $field),
+                max     = $.isNumeric(options.max) ? options.max : validator.getDynamicOption(options.max, $field),
+                length  = value.length,
+                isValid = true,
+                message = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];
+
+            if ((min && length < parseInt(min, 10)) || (max && length > parseInt(max, 10))) {
+                isValid = false;
+            }
+
+            switch (true) {
+                case (!!min && !!max):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.between, [parseInt(min, 10), parseInt(max, 10)]);
+                    break;
+
+                case (!!min):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.more, parseInt(min, 10));
+                    break;
+
+                case (!!max):
+                    message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.less, parseInt(max, 10));
+                    break;
+
+                default:
+                    break;
             }
 
-            return true;
+            return { valid: isValid, message: message };
         }
     };
 }(window.jQuery));

+ 6 - 5
test/spec.js

@@ -277,12 +277,12 @@ describe('enable validators', function() {
                         stringLength: {
                             min: 8,
                             max: 40,
-                            message: 'The full name must be more than 8 and less than 40 characters long'
+                            message: 'The full name must be more than %s and less than %s characters long'
                         },
                         regexp: {
                             enabled: false,
                             regexp: /^[a-zA-Z\s]+$/,
-                            message: 'The username can only consist of alphabetical, number, and space'
+                            message: 'The full name can only consist of alphabetical, number, and space'
                         }
                     }
                 }
@@ -336,8 +336,9 @@ describe('enable validators', function() {
         expect(this.bv.isValid()).toEqual(false);
 
         var messages = this.bv.getMessages('fullName');
+        console.log(messages);
         expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1);
-        expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
+        expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
     });
 
     it('disable particular validators', function() {
@@ -357,7 +358,7 @@ describe('enable validators', function() {
         expect(this.bv.isValid()).toBeTruthy();
 
         var messages = this.bv.getMessages('fullName');
-        expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toEqual(-1);
+        expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toEqual(-1);
     });
 });
 
@@ -1234,7 +1235,7 @@ describe('i18n', function() {
         this.bv.resetForm();
         this.$userName.val('123');
         this.bv.validate();
-        expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(i18n.stringLength.getMessage({ min: 6, max: 20 }));
+        expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(format(i18n.stringLength.between, [6, 20]));
 
         this.bv.resetForm();
         this.$userName.val('contain@#$');

+ 5 - 4
test/spec/enable.js

@@ -18,12 +18,12 @@ describe('enable validators', function() {
                         stringLength: {
                             min: 8,
                             max: 40,
-                            message: 'The full name must be more than 8 and less than 40 characters long'
+                            message: 'The full name must be more than %s and less than %s characters long'
                         },
                         regexp: {
                             enabled: false,
                             regexp: /^[a-zA-Z\s]+$/,
-                            message: 'The username can only consist of alphabetical, number, and space'
+                            message: 'The full name can only consist of alphabetical, number, and space'
                         }
                     }
                 }
@@ -77,8 +77,9 @@ describe('enable validators', function() {
         expect(this.bv.isValid()).toEqual(false);
 
         var messages = this.bv.getMessages('fullName');
+        console.log(messages);
         expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1);
-        expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
+        expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
     });
 
     it('disable particular validators', function() {
@@ -98,6 +99,6 @@ describe('enable validators', function() {
         expect(this.bv.isValid()).toBeTruthy();
 
         var messages = this.bv.getMessages('fullName');
-        expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toEqual(-1);
+        expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toEqual(-1);
     });
 });

+ 1 - 1
test/spec/i18n.js

@@ -261,7 +261,7 @@ describe('i18n', function() {
         this.bv.resetForm();
         this.$userName.val('123');
         this.bv.validate();
-        expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(i18n.stringLength.getMessage({ min: 6, max: 20 }));
+        expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(format(i18n.stringLength.between, [6, 20]));
 
         this.bv.resetForm();
         this.$userName.val('contain@#$');