Skip to content

Commit df3436f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96659' into 2.2-develop-pr19
2 parents f79efef + 2bc86d4 commit df3436f

File tree

7 files changed

+159
-13
lines changed

7 files changed

+159
-13
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ define([
1414
/**
1515
* @param {*} postCode
1616
* @param {*} countryId
17+
* @param {Array} postCodesPatterns
1718
* @return {Boolean}
1819
*/
19-
validate: function (postCode, countryId) {
20-
var patterns = window.checkoutConfig.postCodes[countryId],
21-
pattern, regex;
20+
validate: function (postCode, countryId, postCodesPatterns) {
21+
var pattern, regex,
22+
patterns = postCodesPatterns ? postCodesPatterns[countryId] :
23+
window.checkoutConfig.postCodes[countryId];
2224

2325
this.validatedPostCodeExample = [];
2426

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ define([
4242

4343
return {
4444
validateAddressTimeout: 0,
45+
validateZipCodeTimeout: 0,
4546
validateDelay: 2000,
4647

4748
/**
@@ -133,16 +134,20 @@ define([
133134
});
134135
} else {
135136
element.on('value', function () {
137+
clearTimeout(self.validateZipCodeTimeout);
138+
self.validateZipCodeTimeout = setTimeout(function () {
139+
if (element.index === postcodeElementName) {
140+
self.postcodeValidation(element);
141+
} else {
142+
$.each(postcodeElements, function (index, elem) {
143+
self.postcodeValidation(elem);
144+
});
145+
}
146+
}, delay);
147+
136148
if (!formPopUpState.isVisible()) {
137149
clearTimeout(self.validateAddressTimeout);
138150
self.validateAddressTimeout = setTimeout(function () {
139-
if (element.index === postcodeElementName) {
140-
self.postcodeValidation(element);
141-
} else {
142-
$.each(postcodeElements, function (index, elem) {
143-
self.postcodeValidation(elem);
144-
});
145-
}
146151
self.validateFields();
147152
}, delay);
148153
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Block\DataProviders;
9+
10+
use Magento\Framework\Serialize\SerializerInterface;
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
use Magento\Directory\Model\Country\Postcode\Config as PostCodeConfig;
13+
14+
/**
15+
* Provides postcodes patterns into template.
16+
*/
17+
class PostCodesPatternsAttributeData implements ArgumentInterface
18+
{
19+
/**
20+
* @var PostCodeConfig
21+
*/
22+
private $postCodeConfig;
23+
24+
/**
25+
* @var SerializerInterface
26+
*/
27+
private $serializer;
28+
29+
/**
30+
* Constructor
31+
*
32+
* @param PostCodeConfig $postCodeConfig
33+
* @param SerializerInterface $serializer
34+
*/
35+
public function __construct(PostCodeConfig $postCodeConfig, SerializerInterface $serializer)
36+
{
37+
$this->postCodeConfig = $postCodeConfig;
38+
$this->serializer = $serializer;
39+
}
40+
41+
/**
42+
* Get serialized post codes
43+
*
44+
* @return string
45+
*/
46+
public function getSerializedPostCodes(): string
47+
{
48+
return $this->serializer->serialize($this->postCodeConfig->getPostCodes());
49+
}
50+
}

app/code/Magento/Customer/view/frontend/layout/customer_address_form.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<block class="Magento\Customer\Block\Address\Edit" name="customer_address_edit" template="Magento_Customer::address/edit.phtml" cacheable="false">
2121
<arguments>
2222
<argument name="attribute_data" xsi:type="object">Magento\Customer\Block\DataProviders\AddressAttributeData</argument>
23+
<argument name="post_code_config" xsi:type="object">Magento\Customer\Block\DataProviders\PostCodesPatternsAttributeData</argument>
2324
</arguments>
2425
</block>
2526
</referenceContainer>

app/code/Magento/Customer/view/frontend/templates/address/edit.phtml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>"
127127
id="zip"
128128
class="input-text validate-zip-international <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('postcode')) ?>">
129+
<div role="alert" class="message warning" style="display:none">
130+
<span></span>
131+
</div>
129132
</div>
130133
</div>
131134
<div class="field country required">
@@ -184,7 +187,9 @@
184187
<script type="text/x-magento-init">
185188
{
186189
"#form-validate": {
187-
"addressValidation": {}
190+
"addressValidation": {
191+
"postCodes": <?= /* @noEscape */ $block->getPostCodeConfig()->getSerializedPostCodes(); ?>
192+
}
188193
},
189194
"#country": {
190195
"regionUpdater": {

app/code/Magento/Customer/view/frontend/web/js/addressValidation.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,38 @@
55

66
define([
77
'jquery',
8+
'underscore',
9+
'mageUtils',
10+
'mage/translate',
11+
'Magento_Checkout/js/model/postcode-validator',
812
'jquery/ui',
913
'validation'
10-
], function ($) {
14+
], function ($, __, utils, $t, postCodeValidator) {
1115
'use strict';
1216

1317
$.widget('mage.addressValidation', {
1418
options: {
1519
selectors: {
16-
button: '[data-action=save-address]'
20+
button: '[data-action=save-address]',
21+
zip: '#zip',
22+
country: 'select[name="country_id"]:visible'
1723
}
1824
},
1925

26+
zipInput: null,
27+
countrySelect: null,
28+
2029
/**
2130
* Validation creation
31+
*
2232
* @protected
2333
*/
2434
_create: function () {
2535
var button = $(this.options.selectors.button, this.element);
2636

37+
this.zipInput = $(this.options.selectors.zip, this.element);
38+
this.countrySelect = $(this.options.selectors.country, this.element);
39+
2740
this.element.validation({
2841

2942
/**
@@ -36,6 +49,75 @@ define([
3649
form.submit();
3750
}
3851
});
52+
53+
this._addPostCodeValidation();
54+
},
55+
56+
/**
57+
* Add postcode validation
58+
*
59+
* @protected
60+
*/
61+
_addPostCodeValidation: function () {
62+
var self = this;
63+
64+
this.zipInput.on('keyup', __.debounce(function (event) {
65+
var valid = self._validatePostCode(event.target.value);
66+
67+
self._renderValidationResult(valid);
68+
}, 500)
69+
);
70+
71+
this.countrySelect.on('change', function () {
72+
var valid = self._validatePostCode(self.zipInput.val());
73+
74+
self._renderValidationResult(valid);
75+
});
76+
},
77+
78+
/**
79+
* Validate post code value.
80+
*
81+
* @protected
82+
* @param {String} postCode - post code
83+
* @return {Boolean} Whether is post code valid
84+
*/
85+
_validatePostCode: function (postCode) {
86+
var countryId = this.countrySelect.val();
87+
88+
if (postCode === null) {
89+
return true;
90+
}
91+
92+
return postCodeValidator.validate(postCode, countryId, this.options.postCodes);
93+
},
94+
95+
/**
96+
* Renders warning messages for invalid post code.
97+
*
98+
* @protected
99+
* @param {Boolean} valid
100+
*/
101+
_renderValidationResult: function (valid) {
102+
var warnMessage,
103+
alertDiv = this.zipInput.next();
104+
105+
if (!valid) {
106+
warnMessage = $t('Provided Zip/Postal Code seems to be invalid.');
107+
108+
if (postCodeValidator.validatedPostCodeExample.length) {
109+
warnMessage += $t(' Example: ') + postCodeValidator.validatedPostCodeExample.join('; ') + '. ';
110+
}
111+
warnMessage += $t('If you believe it is the right one you can ignore this notice.');
112+
}
113+
114+
alertDiv.children(':first').text(warnMessage);
115+
116+
if (valid) {
117+
alertDiv.hide();
118+
} else {
119+
alertDiv.show();
120+
}
39121
}
40122
});
41123

app/code/Magento/Multishipping/view/frontend/layout/multishipping_checkout_customer_address.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<block class="Magento\Customer\Block\Address\Edit" name="customer_address_edit" template="Magento_Customer::address/edit.phtml" cacheable="false">
1313
<arguments>
1414
<argument name="attribute_data" xsi:type="object">Magento\Customer\Block\DataProviders\AddressAttributeData</argument>
15+
<argument name="post_code_config" xsi:type="object">Magento\Customer\Block\DataProviders\PostCodesPatternsAttributeData</argument>
1516
</arguments>
1617
</block>
1718
</referenceContainer>

0 commit comments

Comments
 (0)