Browse Source

#465: Refactoring choice validator

phuoc 11 years ago
parent
commit
672786db90

+ 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-05 9:39:38 PM
+ * @version     v0.5.0-dev, built on 2014-07-05 9:58:21 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 36 - 20
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-05 9:39:38 PM
+ * @version     v0.5.0-dev, built on 2014-07-05 9:58:21 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -1877,20 +1877,7 @@
         'default': 'Please enter a valid value',
         less: 'Please choose %s options at minimum',
         more: 'Please choose %s options at maximum',
-        between: 'Please choose %s - %s options',
-
-        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.less, [options.min]);
-                case (!!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.more, [options.max]);
-                default:
-                    return this['default'];
-            }
-        }
+        between: 'Please choose %s - %s options'
     });
 
     $.fn.bootstrapValidator.validators.choice = {
@@ -1908,19 +1895,48 @@
          * @param {Object} options Consists of following keys:
          * - 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 numChoices = $field.is('select')
                             ? validator.getFieldElements($field.attr('data-bv-field')).find('option').filter(':selected').length
-                            : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length;
-            if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
-                return false;
+                            : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length,
+                min        = options.min ? ($.isNumeric(options.min) ? options.min : validator.getDynamicOption(options.min, $field)) : null,
+                max        = options.max ? ($.isNumeric(options.max) ? options.max : validator.getDynamicOption(options.max, $field)) : null,
+                isValid    = true;
+
+            if ((min && numChoices < parseInt(min, 10)) || (max && numChoices > parseInt(max, 10))) {
+                isValid = false;
             }
 
-            return true;
+            var message = $.fn.bootstrapValidator.i18n.choice['default'];
+            switch (true) {
+                case (!!min && !!max):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.between, [parseInt(min, 10), parseInt(max, 10)]);
+                    break;
+
+                case (!!min):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.less, parseInt(min, 10));
+                    break;
+
+                case (!!max):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.more, parseInt(max, 10));
+                    break;
+
+                default:
+                    break;
+            }
+
+            return { valid: isValid, message: message };
         }
     };
 }(window.jQuery));

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


+ 35 - 19
src/js/validator/choice.js

@@ -3,20 +3,7 @@
         'default': 'Please enter a valid value',
         less: 'Please choose %s options at minimum',
         more: 'Please choose %s options at maximum',
-        between: 'Please choose %s - %s options',
-
-        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.less, [options.min]);
-                case (!!options.max):
-                    return $.fn.bootstrapValidator.helpers.format(this.more, [options.max]);
-                default:
-                    return this['default'];
-            }
-        }
+        between: 'Please choose %s - %s options'
     });
 
     $.fn.bootstrapValidator.validators.choice = {
@@ -34,19 +21,48 @@
          * @param {Object} options Consists of following keys:
          * - 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 numChoices = $field.is('select')
                             ? validator.getFieldElements($field.attr('data-bv-field')).find('option').filter(':selected').length
-                            : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length;
-            if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
-                return false;
+                            : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length,
+                min        = options.min ? ($.isNumeric(options.min) ? options.min : validator.getDynamicOption(options.min, $field)) : null,
+                max        = options.max ? ($.isNumeric(options.max) ? options.max : validator.getDynamicOption(options.max, $field)) : null,
+                isValid    = true;
+
+            if ((min && numChoices < parseInt(min, 10)) || (max && numChoices > parseInt(max, 10))) {
+                isValid = false;
+            }
+
+            var message = $.fn.bootstrapValidator.i18n.choice['default'];
+            switch (true) {
+                case (!!min && !!max):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.between, [parseInt(min, 10), parseInt(max, 10)]);
+                    break;
+
+                case (!!min):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.less, parseInt(min, 10));
+                    break;
+
+                case (!!max):
+                    message = $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.choice.more, parseInt(max, 10));
+                    break;
+
+                default:
+                    break;
             }
 
-            return true;
+            return { valid: isValid, message: message };
         }
     };
 }(window.jQuery));

+ 2 - 2
test/spec.js

@@ -1286,12 +1286,12 @@ describe('i18n', function() {
         this.bv.resetForm();
         this.$program.eq(0).prop('checked', 'checked');
         this.bv.validate();
-        expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(i18n.choice.getMessage({ min: 2, max: 4 }));
+        expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
 
         this.bv.resetForm();
         this.$program.prop('checked', 'checked');
         this.bv.validate();
-        expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(i18n.choice.getMessage({ min: 2, max: 4 }));
+        expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
     });
 });
 

+ 2 - 2
test/spec/i18n.js

@@ -313,11 +313,11 @@ describe('i18n', function() {
         this.bv.resetForm();
         this.$program.eq(0).prop('checked', 'checked');
         this.bv.validate();
-        expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(i18n.choice.getMessage({ min: 2, max: 4 }));
+        expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
 
         this.bv.resetForm();
         this.$program.prop('checked', 'checked');
         this.bv.validate();
-        expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(i18n.choice.getMessage({ min: 2, max: 4 }));
+        expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
     });
 });