diff --git a/app/code/Magento/Customer/Block/Widget/Dob.php b/app/code/Magento/Customer/Block/Widget/Dob.php
index f29a9fc76f1d265fcc338752a0923cf82d0c6f5d..f8ee8fddf2ea22d0f356e4f9be07fbdeba7aedb8 100644
--- a/app/code/Magento/Customer/Block/Widget/Dob.php
+++ b/app/code/Magento/Customer/Block/Widget/Dob.php
@@ -208,17 +208,17 @@ class Dob extends AbstractWidget
*/
public function getHtmlExtraParams()
{
- $extraParams = [
- "'validate-date-au':true"
- ];
+ $validators = [];
if ($this->isRequired()) {
- $extraParams[] = 'required:true';
+ $validators['required'] = true;
}
- $extraParams = implode(', ', $extraParams);
+ $validators['validate-date'] = [
+ 'dateFormat' => $this->getDateFormat()
+ ];
- return 'data-validate="{' . $extraParams . '}"';
+ return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
}
/**
diff --git a/app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php b/app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php
index 55c8f97307914845d6ab2e2369c7ba12e3b78854..ea4498591e086770e48d11400017700219439814 100644
--- a/app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php
+++ b/app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php
@@ -59,6 +59,16 @@ class DobTest extends \PHPUnit\Framework\TestCase
*/
protected $filterFactory;
+ /**
+ * @var \Magento\Framework\Escaper
+ */
+ private $escaper;
+
+ /**
+ * @var \Magento\Framework\View\Element\Template\Context
+ */
+ private $context;
+
protected function setUp()
{
$zendCacheCore = new \Zend_Cache_Core();
@@ -84,8 +94,14 @@ class DobTest extends \PHPUnit\Framework\TestCase
['localeResolver' => $localeResolver]
);
- $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
- $context->expects($this->any())->method('getLocaleDate')->will($this->returnValue($timezone));
+ $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
+ $this->context->expects($this->any())->method('getLocaleDate')->will($this->returnValue($timezone));
+
+ $this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['escapeHtml'])
+ ->getMock();
+ $this->context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->escaper));
$this->attribute = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
->getMockForAbstractClass();
@@ -102,7 +118,7 @@ class DobTest extends \PHPUnit\Framework\TestCase
->getMock();
$this->_block = new \Magento\Customer\Block\Widget\Dob(
- $context,
+ $this->context,
$this->createMock(\Magento\Customer\Helper\Address::class),
$this->customerMetadata,
$this->createMock(\Magento\Framework\View\Element\Html\Date::class),
@@ -465,22 +481,40 @@ class DobTest extends \PHPUnit\Framework\TestCase
$this->assertNull($this->_block->getMaxDateRange());
}
- public function testGetHtmlExtraParamsWithoutRequiredOption() {
+ public function testGetHtmlExtraParamsWithoutRequiredOption()
+ {
+ $this->escaper->expects($this->any())
+ ->method('escapeHtml')
+ ->with('{"validate-date":{"dateFormat":"M\/d\/yy"}}')
+ ->will($this->returnValue('{"validate-date":{"dateFormat":"M\/d\/yy"}}'));
+
$this->attribute->expects($this->once())
->method("isRequired")
->willReturn(false);
- $this->assertEquals($this->_block->getHtmlExtraParams(), 'data-validate="{\'validate-date-au\':true}"');
+ $this->assertEquals(
+ $this->_block->getHtmlExtraParams(),
+ 'data-validate="{"validate-date":{"dateFormat":"M\/d\/yy"}}"'
+ );
}
- public function testGetHtmlExtraParamsWithRequiredOption() {
+ public function testGetHtmlExtraParamsWithRequiredOption()
+ {
$this->attribute->expects($this->once())
->method("isRequired")
->willReturn(true);
+ $this->escaper->expects($this->any())
+ ->method('escapeHtml')
+ ->with('{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}')
+ ->will($this->returnValue('{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}'));
+
+
+ $this->context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->escaper));
+
$this->assertEquals(
- $this->_block->getHtmlExtraParams(),
- 'data-validate="{\'validate-date-au\':true, required:true}"'
+ 'data-validate="{"required":true,"validate-date":{"dateFormat":"M\/d\/yy"}}"',
+ $this->_block->getHtmlExtraParams()
);
}
}
diff --git a/lib/web/mage/validation.js b/lib/web/mage/validation.js
index aaece677a485d679f1fca0a893dbcd36b5d24735..85158c581aec175b50525e5dbfe59ae58cc5c678 100644
--- a/lib/web/mage/validation.js
+++ b/lib/web/mage/validation.js
@@ -9,6 +9,7 @@
if (typeof define === 'function' && define.amd) {
define([
'jquery',
+ 'moment',
'jquery/ui',
'jquery/validate',
'mage/translate'
@@ -16,7 +17,7 @@
} else {
factory(jQuery);
}
-}(function ($) {
+}(function ($, moment) {
'use strict';
var creditCartTypes, rules, showLabel, originValidateDelegate;
@@ -966,10 +967,10 @@
$.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
],
'validate-date': [
- function (v) {
- var test = new Date(v);
+ function (value, params, additionalParams) {
+ var test = moment(value, additionalParams.dateFormat);
- return $.mage.isEmptyNoTrim(v) || !isNaN(test);
+ return $.mage.isEmptyNoTrim(value) || test.isValid();
},
$.mage.__('Please enter a valid date.')