Skip to content

Commit 72e9e07

Browse files
Merge forwardport of #11067 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11067.patch (created by @joachimVT) based on commit(s): 1. c3ea1f5 2. 0c0393d 3. f0baa28 4. c7f318c Fixed GitHub Issues in 2.3-develop branch: - #4248: Validations not working on customer registration on DOB field. (reported by @skmomemo) - #6350: Frontend: Datepicker/calendar control does not use the store locale (reported by @heldchen) - #6831: Magento 2.1.1 Invalid input date format 'Invalid date' (reported by @Nerogee) - #6858: DatePicker date format does not reflect user's locale (reported by @qubaji) - #9743: Invalid date when customer validate with French locale (reported by @vjacquemin-sqli)
2 parents c5d9a01 + a47b10d commit 72e9e07

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

app/code/Magento/Customer/Block/Widget/Dob.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,14 @@ public function getHtmlId()
208208
*/
209209
public function getHtmlExtraParams()
210210
{
211-
$extraParams = [
212-
"'validate-date-au':true"
213-
];
214-
211+
$validators = [];
215212
if ($this->isRequired()) {
216-
$extraParams[] = 'required:true';
213+
$validators['required'] = true;
217214
}
218-
219-
$extraParams = implode(', ', $extraParams);
220-
221-
return 'data-validate="{' . $extraParams . '}"';
215+
$validators['validate-date'] = [
216+
'dateFormat' => $this->getDateFormat()
217+
];
218+
return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
222219
}
223220

224221
/**

app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ class DobTest extends \PHPUnit\Framework\TestCase
5757
*/
5858
protected $filterFactory;
5959

60+
/**
61+
* @var \Magento\Framework\Escaper
62+
*/
63+
private $escaper;
64+
65+
/**
66+
* @var \Magento\Framework\View\Element\Template\Context
67+
*/
68+
private $context;
69+
6070
protected function setUp()
6171
{
6272
$zendCacheCore = new \Zend_Cache_Core();
@@ -82,8 +92,13 @@ protected function setUp()
8292
['localeResolver' => $localeResolver]
8393
);
8494

85-
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
86-
$context->expects($this->any())->method('getLocaleDate')->will($this->returnValue($timezone));
95+
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
96+
$this->context->expects($this->any())->method('getLocaleDate')->will($this->returnValue($timezone));
97+
$this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
98+
->disableOriginalConstructor()
99+
->setMethods(['escapeHtml'])
100+
->getMock();
101+
$this->context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->escaper));
87102

88103
$this->attribute = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
89104
->getMockForAbstractClass();
@@ -100,7 +115,7 @@ protected function setUp()
100115
->getMock();
101116

102117
$this->_block = new \Magento\Customer\Block\Widget\Dob(
103-
$context,
118+
$this->context,
104119
$this->createMock(\Magento\Customer\Helper\Address::class),
105120
$this->customerMetadata,
106121
$this->createMock(\Magento\Framework\View\Element\Html\Date::class),
@@ -454,25 +469,37 @@ public function testGetMaxDateRangeWithException()
454469
);
455470
$this->assertNull($this->_block->getMaxDateRange());
456471
}
457-
472+
458473
public function testGetHtmlExtraParamsWithoutRequiredOption()
459474
{
475+
$this->escaper->expects($this->any())
476+
->method('escapeHtml')
477+
->with('{"validate-date":{"dateFormat":"M\/d\/yy"}}')
478+
->will($this->returnValue('{"validate-date":{"dateFormat":"M\/d\/yy"}}'));
460479
$this->attribute->expects($this->once())
461480
->method("isRequired")
462481
->willReturn(false);
463482

464-
$this->assertEquals($this->_block->getHtmlExtraParams(), 'data-validate="{\'validate-date-au\':true}"');
483+
$this->assertEquals(
484+
$this->_block->getHtmlExtraParams(),
485+
'data-validate="{"validate-date":{"dateFormat":"M\/d\/yy"}}"'
486+
);
465487
}
466488

467489
public function testGetHtmlExtraParamsWithRequiredOption()
468490
{
469491
$this->attribute->expects($this->once())
470492
->method("isRequired")
471493
->willReturn(true);
494+
$this->escaper->expects($this->any())
495+
->method('escapeHtml')
496+
->with('{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}')
497+
->will($this->returnValue('{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}'));
498+
$this->context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->escaper));
472499

473500
$this->assertEquals(
474-
$this->_block->getHtmlExtraParams(),
475-
'data-validate="{\'validate-date-au\':true, required:true}"'
501+
'data-validate="{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}"',
502+
$this->_block->getHtmlExtraParams()
476503
);
477504
}
478505
}

lib/web/mage/validation.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
if (typeof define === 'function' && define.amd) {
1010
define([
1111
'jquery',
12+
'moment',
1213
'jquery/ui',
1314
'jquery/validate',
1415
'mage/translate'
1516
], factory);
1617
} else {
1718
factory(jQuery);
1819
}
19-
}(function ($) {
20+
}(function ($, moment) {
2021
'use strict';
2122

2223
var creditCartTypes, rules, showLabel, originValidateDelegate;
@@ -988,10 +989,10 @@
988989
$.mage.__('Please use only letters (a-z or A-Z) or numbers (0-9) in this field. No spaces or other characters are allowed.') //eslint-disable-line max-len
989990
],
990991
'validate-date': [
991-
function (v) {
992-
var test = new Date(v);
992+
function (value, params, additionalParams) {
993+
var test = moment(value, additionalParams.dateFormat);
993994

994-
return $.mage.isEmptyNoTrim(v) || !isNaN(test);
995+
return $.mage.isEmptyNoTrim(value) || test.isValid();
995996
},
996997
$.mage.__('Please enter a valid date.')
997998

0 commit comments

Comments
 (0)