ソースを参照

#465: Refactoring zipCode validator

phuoc 11 年 前
コミット
883aca102e

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

+ 83 - 44
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 6:42:43 PM
+ * @version     v0.5.0-dev, built on 2014-07-05 9:27:24 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -1368,6 +1368,50 @@
         },
 
         /**
+         * Some other validators have option which its value is dynamic.
+         * For example, the zipCode validator which country is set by a select element.
+         *
+         * @param {String|Function} option The option which can be determined by:
+         * - a string
+         * - name of field which defines the value
+         * - name of function which returns the value
+         * - a function returns the value
+         *
+         * The callback function has the format of
+         *      callback: function(value, validator, $field) {
+         *          // value is the value of field
+         *          // validator is the BootstrapValidator instance
+         *          // $field is the field element
+         *      }
+         *
+         * @param {jQuery|String} field The field name or element
+         * @returns {String}
+         */
+        getDynamicOption: function(option, field) {
+            var $field = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                value  = field.val();
+
+            // Option can be determined by
+            // ... a function
+            if ('function' === typeof option) {
+                return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]);
+            }
+            // ... value of other field
+            else if ('string' === typeof option) {
+                var $f = this.getFieldElements(option);
+                if ($f.length) {
+                    return $f.val();
+                }
+                // ... return value of callback
+                else {
+                    return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]);
+                }
+            }
+
+            return null;
+        },
+
+        /**
          * Destroy the plugin
          * It will remove all error messages, feedback icons and turn off the events
          */
@@ -6162,6 +6206,7 @@
 ;(function($) {
     $.fn.bootstrapValidator.i18n.zipCode = $.extend($.fn.bootstrapValidator.i18n.zipCode || {}, {
         'default': 'Please enter a valid zip code',
+        countryNotSupported: 'The country code %s is not supported',
         country: 'Please enter a valid %s',
         countries: {
             'CA': 'Canadian postal code',
@@ -6172,15 +6217,6 @@
             'SE': 'Swiss postal code',
             'SG': 'Singapore postal code',
             'US': 'US zip code'
-        },
-
-        getMessage: function(options) {
-            var country = options.country;
-            if ('string' === typeof country && this.countries[country]) {
-                return $.fn.bootstrapValidator.helpers.format(this.country, this.countries[country]);
-            }
-
-            return this['default'];
         }
     });
 
@@ -6223,7 +6259,7 @@
          *      // $field is jQuery element representing the field
          * }
          *
-         * @returns {Boolean}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -6232,53 +6268,56 @@
             }
 
             var country = options.country;
-            switch (typeof country) {
-                case 'function':
-                    country = $.fn.bootstrapValidator.helpers.call(country, [value, validator, $field]);
-                    break;
-
-                case 'string':
-                /* falls through */
-                default:
-                    if ($.inArray(country, this.COUNTRIES) === -1) {
-                        // Try to indicate the field which its value define the country code
-                        var $countryField = validator.getFieldElements(country);
-                        if ($countryField && $countryField.length) {
-                            country = $countryField.val();
-                        }
-                        // Try to get the country code via a callback
-                        else {
-                            country = $.fn.bootstrapValidator.helpers.call(country, [value, validator, $field]);
-                        }
-                    }
-                    break;
+            if (typeof country !== 'string' || $.inArray(country, this.COUNTRIES) === -1) {
+                // Try to determine the country
+                country = validator.getDynamicOption(country, $field);
             }
 
-            if (!country) {
-                return false;
+            if (!country || $.inArray(country.toUpperCase(), this.COUNTRIES) === -1) {
+                return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, country) };
             }
 
+            var isValid = false;
             country = country.toUpperCase();
-            if ($.inArray(country, this.COUNTRIES) === -1) {
-                return false;
-            }
             switch (country) {
-                case 'CA': return /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}$/i.test(value);
-                case 'DK': return /^(DK(-|\s)?)?\d{4}$/i.test(value);
-                case 'GB': return this._gb(value);
+                case 'CA':
+                    isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}$/i.test(value);
+                    break;
+
+                case 'DK':
+                    isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                    break;
+
+                case 'GB':
+                    isValid = this._gb(value);
+                    break;
 
                 // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
-                case 'IT': return /^(I-|IT-)?\d{5}$/i.test(value);
+                case 'IT':
+                    isValid = /^(I-|IT-)?\d{5}$/i.test(value);
+                    break;
 
                 // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
-                case 'NL': return /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
+                case 'NL':
+                    isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
+                    break;
+
+                case 'SE':
+                    isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
+                    break;
+
+                case 'SG':
+                    isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
+                    break;
 
-                case 'SE': return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
-                case 'SG': return /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
                 case 'US':
                 /* falls through */
-                default: return /^\d{4,5}([\-]?\d{4})?$/.test(value);
+                default:
+                    isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
+                    break;
             }
+
+            return { valid: isValid, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country]) };
         },
 
         /**

ファイルの差分が大きいため隠しています
+ 4 - 4
dist/js/bootstrapValidator.min.js


+ 44 - 0
src/js/bootstrapValidator.js

@@ -1367,6 +1367,50 @@
         },
 
         /**
+         * Some other validators have option which its value is dynamic.
+         * For example, the zipCode validator which country is set by a select element.
+         *
+         * @param {String|Function} option The option which can be determined by:
+         * - a string
+         * - name of field which defines the value
+         * - name of function which returns the value
+         * - a function returns the value
+         *
+         * The callback function has the format of
+         *      callback: function(value, validator, $field) {
+         *          // value is the value of field
+         *          // validator is the BootstrapValidator instance
+         *          // $field is the field element
+         *      }
+         *
+         * @param {jQuery|String} field The field name or element
+         * @returns {String}
+         */
+        getDynamicOption: function(option, field) {
+            var $field = ('string' === typeof field) ? this.getFieldElements(field) : field,
+                value  = field.val();
+
+            // Option can be determined by
+            // ... a function
+            if ('function' === typeof option) {
+                return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]);
+            }
+            // ... value of other field
+            else if ('string' === typeof option) {
+                var $f = this.getFieldElements(option);
+                if ($f.length) {
+                    return $f.val();
+                }
+                // ... return value of callback
+                else {
+                    return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]);
+                }
+            }
+
+            return null;
+        },
+
+        /**
          * Destroy the plugin
          * It will remove all error messages, feedback icons and turn off the events
          */

+ 38 - 43
src/js/validator/zipCode.js

@@ -1,6 +1,7 @@
 (function($) {
     $.fn.bootstrapValidator.i18n.zipCode = $.extend($.fn.bootstrapValidator.i18n.zipCode || {}, {
         'default': 'Please enter a valid zip code',
+        countryNotSupported: 'The country code %s is not supported',
         country: 'Please enter a valid %s',
         countries: {
             'CA': 'Canadian postal code',
@@ -11,15 +12,6 @@
             'SE': 'Swiss postal code',
             'SG': 'Singapore postal code',
             'US': 'US zip code'
-        },
-
-        getMessage: function(options) {
-            var country = options.country;
-            if ('string' === typeof country && this.countries[country]) {
-                return $.fn.bootstrapValidator.helpers.format(this.country, this.countries[country]);
-            }
-
-            return this['default'];
         }
     });
 
@@ -62,7 +54,7 @@
          *      // $field is jQuery element representing the field
          * }
          *
-         * @returns {Boolean}
+         * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -71,53 +63,56 @@
             }
 
             var country = options.country;
-            switch (typeof country) {
-                case 'function':
-                    country = $.fn.bootstrapValidator.helpers.call(country, [value, validator, $field]);
-                    break;
-
-                case 'string':
-                /* falls through */
-                default:
-                    if ($.inArray(country, this.COUNTRIES) === -1) {
-                        // Try to indicate the field which its value define the country code
-                        var $countryField = validator.getFieldElements(country);
-                        if ($countryField && $countryField.length) {
-                            country = $countryField.val();
-                        }
-                        // Try to get the country code via a callback
-                        else {
-                            country = $.fn.bootstrapValidator.helpers.call(country, [value, validator, $field]);
-                        }
-                    }
-                    break;
+            if (typeof country !== 'string' || $.inArray(country, this.COUNTRIES) === -1) {
+                // Try to determine the country
+                country = validator.getDynamicOption(country, $field);
             }
 
-            if (!country) {
-                return false;
+            if (!country || $.inArray(country.toUpperCase(), this.COUNTRIES) === -1) {
+                return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, country) };
             }
 
+            var isValid = false;
             country = country.toUpperCase();
-            if ($.inArray(country, this.COUNTRIES) === -1) {
-                return false;
-            }
             switch (country) {
-                case 'CA': return /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}$/i.test(value);
-                case 'DK': return /^(DK(-|\s)?)?\d{4}$/i.test(value);
-                case 'GB': return this._gb(value);
+                case 'CA':
+                    isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}$/i.test(value);
+                    break;
+
+                case 'DK':
+                    isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                    break;
+
+                case 'GB':
+                    isValid = this._gb(value);
+                    break;
 
                 // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
-                case 'IT': return /^(I-|IT-)?\d{5}$/i.test(value);
+                case 'IT':
+                    isValid = /^(I-|IT-)?\d{5}$/i.test(value);
+                    break;
 
                 // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
-                case 'NL': return /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
+                case 'NL':
+                    isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
+                    break;
+
+                case 'SE':
+                    isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
+                    break;
+
+                case 'SG':
+                    isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
+                    break;
 
-                case 'SE': return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
-                case 'SG': return /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
                 case 'US':
                 /* falls through */
-                default: return /^\d{4,5}([\-]?\d{4})?$/.test(value);
+                default:
+                    isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
+                    break;
             }
+
+            return { valid: isValid, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country]) };
         },
 
         /**