Browse Source

#447: zipCode validator allows to set the country code via another field or callback, thanks to @AlaskanShade

nghuuphuoc 11 years ago
parent
commit
43d42e3093

+ 2 - 1
CHANGELOG.md

@@ -49,6 +49,7 @@ __Improvements__
 * [#422](https://github.com/nghuuphuoc/bootstrapvalidator/issues/422): Exclude particular field by ```excluded``` option or ```data-bv-excluded``` attribute
 * [#431](https://github.com/nghuuphuoc/bootstrapvalidator/issues/431): Add built time to the build file
 * [#432](https://github.com/nghuuphuoc/bootstrapvalidator/issues/432): Define the callback via ```data-bv-callback-callback``` attribute
+* [#447](https://github.com/nghuuphuoc/bootstrapvalidator/pull/447): [zipCode validator](http://bootstrapvalidator.com/validators/zipCode/) allow to set the country code via another field or callback, thanks to [@AlaskanShade](https://github.com/AlaskanShade)
 
 __Bug Fixes__
 * [#288](https://github.com/nghuuphuoc/bootstrapvalidator/issues/288): Fix [date validator](http://bootstrapvalidator.com/validators/date/) issue on IE8
@@ -67,7 +68,7 @@ __Bug Fixes__
 * [#401](https://github.com/nghuuphuoc/bootstrapvalidator/issues/401): [stringLength validator](http://bootstrapvalidator.com/validators/stringLength/) allows spaces after max length
 * [#411](https://github.com/nghuuphuoc/bootstrapvalidator/pull/411): Fix the [ean validator](http://bootstrapvalidator.com/validators/ean/) when the check digit is zero, thanks to [@manish-in-java](https://github.com/manish-in-java)
 * [#417](https://github.com/nghuuphuoc/bootstrapvalidator/issues/417): IPv6 validator doesn't work
-* [#447](https://github.com/nghuuphuoc/bootstrapvalidator/pull/447): Skip the ```_isExcluded()``` when initializing the form. This fixes [#273](https://github.com/nghuuphuoc/bootstrapvalidator/issues/273). Thanks to [@AlaskanShade](https://github.com/AlaskanShade)
+* [#447](https://github.com/nghuuphuoc/bootstrapvalidator/pull/447): Skip the ```_isExcluded()``` when initializing the form. This fixes [#269](https://github.com/nghuuphuoc/bootstrapvalidator/issues/269), [#273](https://github.com/nghuuphuoc/bootstrapvalidator/issues/273). Thanks to [@AlaskanShade](https://github.com/AlaskanShade)
 
 __Document__
 * [#259](https://github.com/nghuuphuoc/bootstrapvalidator/issues/259): Typo "Support almost Bootstrap forms", thanks to [@lloydde](https://github.com/lloydde)

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

+ 49 - 16
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-01 8:05:04 AM
+ * @version     v0.5.0-dev, built on 2014-07-01 8:44:14 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -6113,8 +6113,7 @@
     $.fn.bootstrapValidator.validators.zipCode = {
         html5Attributes: {
             message: 'message',
-            country: 'country',
-            countryfield: 'countryfield'
+            country: 'country'
         },
 
         COUNTRIES: ['CA', 'DK', 'GB', 'IT', 'NL', 'SE', 'SG', 'US'],
@@ -6126,28 +6125,62 @@
          * @param {jQuery} $field Field element
          * @param {Object} options Consist of key:
          * - message: The invalid message
-         * - country: The ISO 3166 country code
-         * - counryfield: Another field that contains the country code
+         * - country: The country
          *
+         * The country can be defined by:
+         * - An ISO 3166 country code
          * Currently it supports the following countries:
-         * - US (United State)
-         * - CA (Canada)
-         * - DK (Denmark)
-         * - GB (United Kingdom)
-         * - IT (Italy)
-         * - NL (Netherlands)
-         * - SE (Sweden)
-         * - SG (Singapore)
+         *      - US (United State)
+         *      - CA (Canada)
+         *      - DK (Denmark)
+         *      - GB (United Kingdom)
+         *      - IT (Italy)
+         *      - NL (Netherlands)
+         *      - SE (Sweden)
+         *      - SG (Singapore)
+         *
+         * - 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
+         *
+         * callback: function(value, validator, $field) {
+         *      // value is the value of field
+         *      // validator is the BootstrapValidator instance
+         *      // $field is jQuery element representing the field
+         * }
+         *
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
-            if (value === '' || (!options.country && !options.countryfield)) {
+            if (value == '' || !options.country) {
                 return true;
             }
 
-            var cField = options.countryfield ? validator.getFieldElements(options.countryfield) : null;
-            var country = (cField ? cField.val() : null || options.country || 'US').toUpperCase();
+            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;
+            }
+
+            country = country.toUpperCase();
             if ($.inArray(country, this.COUNTRIES) === -1) {
                 return false;
             }

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


+ 48 - 15
src/js/validator/zipCode.js

@@ -31,8 +31,7 @@
     $.fn.bootstrapValidator.validators.zipCode = {
         html5Attributes: {
             message: 'message',
-            country: 'country',
-            countryfield: 'countryfield'
+            country: 'country'
         },
 
         COUNTRIES: ['CA', 'DK', 'GB', 'IT', 'NL', 'SE', 'SG', 'US'],
@@ -44,28 +43,62 @@
          * @param {jQuery} $field Field element
          * @param {Object} options Consist of key:
          * - message: The invalid message
-         * - country: The ISO 3166 country code
-         * - counryfield: Another field that contains the country code
+         * - country: The country
          *
+         * The country can be defined by:
+         * - An ISO 3166 country code
          * Currently it supports the following countries:
-         * - US (United State)
-         * - CA (Canada)
-         * - DK (Denmark)
-         * - GB (United Kingdom)
-         * - IT (Italy)
-         * - NL (Netherlands)
-         * - SE (Sweden)
-         * - SG (Singapore)
+         *      - US (United State)
+         *      - CA (Canada)
+         *      - DK (Denmark)
+         *      - GB (United Kingdom)
+         *      - IT (Italy)
+         *      - NL (Netherlands)
+         *      - SE (Sweden)
+         *      - SG (Singapore)
+         *
+         * - 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
+         *
+         * callback: function(value, validator, $field) {
+         *      // value is the value of field
+         *      // validator is the BootstrapValidator instance
+         *      // $field is jQuery element representing the field
+         * }
+         *
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
-            if (value === '' || (!options.country && !options.countryfield)) {
+            if (value == '' || !options.country) {
                 return true;
             }
 
-            var cField = options.countryfield ? validator.getFieldElements(options.countryfield) : null;
-            var country = (cField ? cField.val() : null || options.country || 'US').toUpperCase();
+            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;
+            }
+
+            country = country.toUpperCase();
             if ($.inArray(country, this.COUNTRIES) === -1) {
                 return false;
             }