Skip to content

Commit 685254d

Browse files
ENGCOM-6057: adding new validation for future dates to admin in customer DOB - issue #22692 #24588
2 parents b02fb34 + 5b37560 commit 685254d

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ public function getHtmlExtraParams()
267267
$validators['validate-date'] = [
268268
'dateFormat' => $this->getDateFormat()
269269
];
270+
$validators['validate-dob'] = true;
271+
270272
return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
271273
}
272274

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
use Magento\Customer\Api\CustomerMetadataInterface;
1010
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1111
use Magento\Customer\Api\Data\ValidationRuleInterface;
12+
use Magento\Customer\Block\Widget\Dob;
1213
use Magento\Customer\Helper\Address;
1314
use Magento\Framework\App\CacheInterface;
1415
use Magento\Framework\Cache\FrontendInterface;
1516
use Magento\Framework\Data\Form\FilterFactory;
1617
use Magento\Framework\Escaper;
1718
use Magento\Framework\Exception\NoSuchEntityException;
18-
use Magento\Customer\Block\Widget\Dob;
1919
use Magento\Framework\Locale\Resolver;
2020
use Magento\Framework\Locale\ResolverInterface;
2121
use Magento\Framework\Stdlib\DateTime\Timezone;
@@ -536,16 +536,16 @@ public function testGetHtmlExtraParamsWithoutRequiredOption()
536536
{
537537
$this->escaper->expects($this->any())
538538
->method('escapeHtml')
539-
->with('{"validate-date":{"dateFormat":"M\/d\/Y"}}')
540-
->will($this->returnValue('{"validate-date":{"dateFormat":"M\/d\/Y"}}'));
539+
->with('{"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}')
540+
->will($this->returnValue('{"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}'));
541541

542542
$this->attribute->expects($this->once())
543543
->method("isRequired")
544544
->willReturn(false);
545545

546546
$this->assertEquals(
547547
$this->_block->getHtmlExtraParams(),
548-
'data-validate="{"validate-date":{"dateFormat":"M\/d\/Y"}}"'
548+
'data-validate="{"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}"'
549549
);
550550
}
551551

@@ -559,13 +559,17 @@ public function testGetHtmlExtraParamsWithRequiredOption()
559559
->willReturn(true);
560560
$this->escaper->expects($this->any())
561561
->method('escapeHtml')
562-
->with('{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"}}')
563-
->will($this->returnValue('{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"}}'));
562+
->with('{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}')
563+
->will(
564+
$this->returnValue(
565+
'{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}'
566+
)
567+
);
564568

565569
$this->context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->escaper));
566570

567571
$this->assertEquals(
568-
'data-validate="{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"}}"',
572+
'data-validate="{"required":true,"validate-date":{"dateFormat":"M\/d\/Y"},"validate-dob":true}"',
569573
$this->_block->getHtmlExtraParams()
570574
);
571575
}

app/code/Magento/Customer/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,4 @@ Addresses,Addresses
539539
"Prefix","Prefix"
540540
"Middle Name/Initial","Middle Name/Initial"
541541
"Suffix","Suffix"
542+
"The Date of Birth should not be greater than today.","The Date of Birth should not be greater than today."

app/code/Magento/Customer/view/base/ui_component/customer_form.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,20 @@
265265
<settings>
266266
<validation>
267267
<rule name="validate-date" xsi:type="boolean">true</rule>
268+
<rule name="validate-dob" xsi:type="boolean">true</rule>
268269
</validation>
269270
<dataType>text</dataType>
270271
<visible>true</visible>
271272
</settings>
273+
<formElements>
274+
<date>
275+
<settings>
276+
<options>
277+
<option name="maxDate" xsi:type="string">-1d</option>
278+
</options>
279+
</settings>
280+
</date>
281+
</formElements>
272282
</field>
273283
<field name="taxvat" formElement="input">
274284
<argument name="data" xsi:type="array">

app/code/Magento/Customer/view/frontend/templates/widget/dob.phtml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ $fieldCssClass .= $block->isRequired() ? ' required' : '';
3535
<?php endif; ?>
3636
</div>
3737
</div>
38+
39+
<script type="text/x-magento-init">
40+
{
41+
"*": {
42+
"Magento_Customer/js/validation": {}
43+
}
44+
}
45+
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
define([
2+
'jquery',
3+
'moment',
4+
'jquery/validate',
5+
'mage/translate'
6+
], function ($, moment) {
7+
'use strict';
8+
9+
$.validator.addMethod(
10+
'validate-dob',
11+
function (value) {
12+
if (value === '') {
13+
return true;
14+
}
15+
16+
return moment(value).isBefore(moment());
17+
},
18+
$.mage.__('The Date of Birth should not be greater than today.')
19+
);
20+
});

app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,16 @@ define([
10671067
return new RegExp(param).test(value);
10681068
},
10691069
$.mage.__('This link is not allowed.')
1070+
],
1071+
'validate-dob': [
1072+
function (value) {
1073+
if (value === '') {
1074+
return true;
1075+
}
1076+
1077+
return moment(value).isBefore(moment());
1078+
},
1079+
$.mage.__('The Date of Birth should not be greater than today.')
10701080
]
10711081
}, function (data) {
10721082
return {

0 commit comments

Comments
 (0)