-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Fix the wrong input format of Customer date of birth #11332
Comments
@selusi, thank you for your report. |
Can i work in this issue? Thanks! |
Internal ticket to track issue progress: MAGETWO-81580 |
Hi @selusi the issue has been fixed in 2.2-develop branch and will be available in 2.2.2 soon |
Hi @selusi. Thank you for your report.
The fix will be available with the upcoming 2.3.0 release. |
To solve this issue need to modify or override below file. vendor\magento\module-customer\Block\Widget\Dob.php
changes in above function as below
Updated
It fix the validation message of dob "please enter valid date" in customer registration and edit page in front-end. |
As of Magento 2.3.5-p1, the relevant method looks like this: public function getHtmlExtraParams()
{
$validators = [];
if ($this->isRequired()) {
$validators['required'] = true;
}
$validators['validate-date'] = [
'dateFormat' => $this->getDateFormat()
];
$validators['validate-dob'] = true;
return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
} The same change as above works, as long as the new line of code is also removed; that is, change $validators['validate-date'] = [
'dateFormat' => $this->getDateFormat()
];
$validators['validate-dob'] = true; to $validators['validate-dob'] = [
'dateFormat' => $this->getDateFormat()
]; Here's the complete override using the preference mechanism, in a new custom module in registration.php <?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'StoreName_Customer',
__DIR__
); composer.json {
"name": "storename/customer",
"description": "StoreName extension to Magento Customer module",
"require": {
"php": "~5.5.0|~5.6.0|^7.0.0",
"magento/module-customer": "^1.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [ "registration.php" ]
}
} etc/module.xml <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="StoreName_Customer" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer" />
</sequence>
</module>
</config> etc/di.xml <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Block\Widget\Dob" type="StoreName\Customer\Block\Widget\Dob" />
</config> Block/Widget/Dob.php <?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace StoreName\Customer\Block\Widget;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Framework\Api\ArrayObjectSearch;
/**
* Class Dob
*
* @SuppressWarnings(PHPMD.DepthOfInheritance)
*/
class Dob extends \Magento\Customer\Block\Widget\Dob
{
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Helper\Address $addressHelper
* @param CustomerMetadataInterface $customerMetadata
* @param \Magento\Framework\View\Element\Html\Date $dateElement
* @param \Magento\Framework\Data\Form\FilterFactory $filterFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Helper\Address $addressHelper,
CustomerMetadataInterface $customerMetadata,
\Magento\Framework\View\Element\Html\Date $dateElement,
\Magento\Framework\Data\Form\FilterFactory $filterFactory,
array $data = []
) {
parent::__construct($context, $addressHelper, $customerMetadata, $dateElement, $filterFactory, $data);
}
/**
* @inheritdoc
*/
public function _construct()
{
parent::_construct();
}
/**
* Sanitizes time
*
* @param mixed $value
* @return bool|int
*/
private function filterTime($value)
{
$time = false;
if ($value) {
if ($value instanceof \DateTimeInterface) {
$time = $value->getTimestamp();
} elseif (is_numeric($value)) {
$time = $value;
} elseif (is_string($value)) {
$time = strtotime($value);
$time = $time === false ? $this->_localeDate->date($value, null, false, false)->getTimestamp() : $time;
}
}
return $time;
}
/**
* Return data-validate rules
*
* @return string
*/
public function getHtmlExtraParams()
{
$validators = [];
if ($this->isRequired()) {
$validators['required'] = true;
}
$validators['validate-dob'] = [
'dateFormat' => $this->getDateFormat()
];
return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
}
} |
This bug is still present in Magento 2.3.5-p1 EE The code suggested by @lewisje fixes the issue on the customer registration page, otherwise the system will only let people born after 2000 register.... #ageism! |
To be clear, @jmonrova, customers could still register without entering a DoB, as long as that field is not made mandatory (which it isn't by default). |
Preconditions
Steps to reproduce
Expected result
Actual result
To fix the issue
To solve the issue have to modify the file:
vendor\magento\module-customer\Block\Widget\Dob.php
Actually the line 113 is:
(that return MM/dd/yy format in English locale)
This format is good for javascript datepicker configuration but it is wrong for the form input text.
I wrote a method that return the correct date format:
the new 113 line:
Obviously this is my code though not very elegant, but it solves the problem.
The text was updated successfully, but these errors were encountered: