Skip to content

Commit 7a20e8b

Browse files
author
Stanislav Idolov
committed
MAGETWO-44385: Zip code format validation/hint isn't working per Country
1 parent 746bff6 commit 7a20e8b

File tree

1 file changed

+57
-26
lines changed

1 file changed

+57
-26
lines changed

app/code/Magento/Checkout/view/frontend/web/js/model/shipping-rates-validator.js

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,46 @@ define(
1515
],
1616
function ($, ko, shippingRatesValidationRules, addressConverter, selectShippingAddress, postcodeValidator, $t) {
1717
'use strict';
18-
var checkoutConfig = window.checkoutConfig;
19-
var validators = [];
20-
var observedElements = [];
21-
var postcodeElement = null;
18+
19+
var checkoutConfig = window.checkoutConfig,
20+
validators = [],
21+
observedElements = [],
22+
postcodeElement = null;
2223

2324
return {
2425
validateAddressTimeout: 0,
2526
validateDelay: 2000,
2627

27-
registerValidator: function(carrier, validator) {
28+
/**
29+
* @param {String} carrier
30+
* @param {Object} validator
31+
*/
32+
registerValidator: function (carrier, validator) {
2833
if (checkoutConfig.activeCarriers.indexOf(carrier) != -1) {
2934
validators.push(validator);
3035
}
3136
},
3237

33-
validateAddressData: function(address) {
38+
/**
39+
* @param {Object} address
40+
* @return {Boolean}
41+
*/
42+
validateAddressData: function (address) {
3443
return validators.some(function(validator) {
3544
return validator.validate(address);
3645
});
3746
},
3847

39-
bindChangeHandlers: function(elements, force, delay) {
40-
var self = this;
41-
var observableFields = shippingRatesValidationRules.getObservableFields();
42-
$.each(elements, function(index, elem) {
48+
/**
49+
* @param {*} elements
50+
* @param {Boolean} force
51+
* @param {Number} delay
52+
*/
53+
bindChangeHandlers: function (elements, force, delay) {
54+
var self = this,
55+
observableFields = shippingRatesValidationRules.getObservableFields();
56+
57+
$.each(elements, function (index, elem) {
4358
if (elem && (observableFields.indexOf(elem.index) != -1 || force)) {
4459
if (elem.index !== 'postcode') {
4560
self.bindHandler(elem, delay);
@@ -53,17 +68,23 @@ define(
5368
});
5469
},
5570

56-
bindHandler: function(element, delay) {
71+
/**
72+
* @param {Object} element
73+
* @param {Number} delay
74+
*/
75+
bindHandler: function (element, delay) {
5776
var self = this;
77+
5878
delay = typeof delay === "undefined" ? self.validateDelay : delay;
79+
5980
if (element.component.indexOf('/group') != -1) {
60-
$.each(element.elems(), function(index, elem) {
81+
$.each(element.elems(), function (index, elem) {
6182
self.bindHandler(elem);
6283
});
6384
} else {
64-
element.on('value', function() {
85+
element.on('value', function () {
6586
clearTimeout(self.validateAddressTimeout);
66-
self.validateAddressTimeout = setTimeout(function() {
87+
self.validateAddressTimeout = setTimeout(function () {
6788
if (self.postcodeValidation()) {
6889
self.validateFields();
6990
}
@@ -73,36 +94,44 @@ define(
7394
}
7495
},
7596

76-
postcodeValidation: function() {
97+
/**
98+
* @return {*}
99+
*/
100+
postcodeValidation: function () {
101+
var countryId = $('select[name="country_id"]').val(),
102+
validationResult = postcodeValidator.validate(postcodeElement.value(), countryId),
103+
warnMessage;
104+
77105
if (postcodeElement == null || postcodeElement.value() == null) {
78106
return true;
79107
}
80108

81-
var countryId = $('select[name="shippingAddress[country_id]"]').val();
82-
var validationResult = postcodeValidator.validate(postcodeElement.value(), countryId);
83-
84109
postcodeElement.warn(null);
110+
85111
if (!validationResult) {
86-
var warnMessage = $t('Provided Zip/Postal Code seems to be invalid.');
112+
warnMessage = $t('Provided Zip/Postal Code seems to be invalid.');
87113
if (postcodeValidator.validatedPostCodeExample.length) {
88114
warnMessage += $t(' Example: ') + postcodeValidator.validatedPostCodeExample.join('; ') + '. ';
89115
}
90116
warnMessage += $t('If you believe it is the right one you can ignore this notice.');
91117
postcodeElement.warn(warnMessage);
92118
}
119+
93120
return validationResult;
94121
},
95122

96123
/**
97124
* Convert form data to quote address and validate fields for shipping rates
98125
*/
99-
validateFields: function() {
126+
validateFields: function () {
100127
var addressFlat = addressConverter.formDataProviderToFlatData(
101-
this.collectObservedData(),
102-
'shippingAddress'
103-
);
128+
this.collectObservedData(),
129+
'shippingAddress'
130+
),
131+
address;
132+
104133
if (this.validateAddressData(addressFlat)) {
105-
var address = addressConverter.formAddressDataToQuoteAddress(addressFlat);
134+
address = addressConverter.formAddressDataToQuoteAddress(addressFlat);
106135
selectShippingAddress(address);
107136
}
108137
},
@@ -112,11 +141,13 @@ define(
112141
*
113142
* @returns {*}
114143
*/
115-
collectObservedData: function() {
144+
collectObservedData: function () {
116145
var observedValues = {};
117-
$.each(observedElements, function(index, field) {
146+
147+
$.each(observedElements, function (index, field) {
118148
observedValues[field.dataScope] = field.value();
119149
});
150+
120151
return observedValues;
121152
}
122153
};

0 commit comments

Comments
 (0)