Skip to content
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

Closed
selusi opened this issue Oct 10, 2017 · 9 comments
Closed

How to Fix the wrong input format of Customer date of birth #11332

selusi opened this issue Oct 10, 2017 · 9 comments
Assignees
Labels
Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@selusi
Copy link

selusi commented Oct 10, 2017

Preconditions

  1. PHP 7.0.22
  2. MySql 5.7
  3. Apache 2.4
  4. Magento 2.2.0

Steps to reproduce

  1. Login to frontend
  2. Edit Customer information

Expected result

  1. The year of birth date should be in the format: yyyy.

Actual result

  1. The actual format is yy

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:

$data['format'] = $this->getDateFormat(); 

(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:

protected function getDateFormatForInput()
{
    $formatArr = explode(' ', preg_replace("/[^a-zA-Z]/",' ',$this->getDateFormat())) ;
    $reseteFormat = array();
    foreach($formatArr as $k){
        if($k != ''){
            if(substr($k,0,1) == 'y' ){
                $reseteFormat[] = 'yyyy';
            } else {
                $reseteFormat[] = $k;
            }
        }
    }
    return implode("/", $reseteFormat);
}

the new 113 line:

$data['format'] = $this->getDateFormatForInput(); 

Obviously this is my code though not very elegant, but it solves the problem.

@magento-engcom-team magento-engcom-team added Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed labels Oct 10, 2017
@magento-engcom-team
Copy link
Contributor

@selusi, thank you for your report.
We've created internal ticket(s) MAGETWO-81452 to track progress on the issue.

@magento-engcom-team magento-engcom-team added 2.2.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Oct 10, 2017
@manuelson
Copy link
Contributor

Can i work in this issue? Thanks!

@magento-team
Copy link
Contributor

Internal ticket to track issue progress: MAGETWO-81580

@okorshenko
Copy link
Contributor

Hi @selusi the issue has been fixed in 2.2-develop branch and will be available in 2.2.2 soon

@okorshenko okorshenko added the Fixed in 2.2.x The issue has been fixed in 2.2 release line label Oct 17, 2017
@magento-engcom-team
Copy link
Contributor

Hi @selusi. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1361 by @magento-engcom-team in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Feb 8, 2018
@brana946
Copy link

brana946 commented Sep 20, 2019

To solve this issue need to modify or override below file.

vendor\magento\module-customer\Block\Widget\Dob.php

public function getHtmlExtraParams()
{
    $validators = [];
    if ($this->isRequired()) {
        $validators['required'] = true;
    }
    $validators['validate-date'] = [
        'dateFormat' => $this->getDateFormat()
    ];
    return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
}

changes in above function as below
Old

    $validators['validate-date'] = [
        'dateFormat' => $this->getDateFormat()
    ];

Updated

    $validators['validate-dob'] = [
        'dateFormat' => $this->getDateFormat()
    ];

It fix the validation message of dob "please enter valid date" in customer registration and edit page in front-end.

@lewisje
Copy link

lewisje commented May 7, 2020

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 app/code/StoreName/Customer:

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)) . '"';
    }
}

@jmonrove
Copy link

jmonrove commented Jun 17, 2020

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!
@magento-engcom-team

@lewisje
Copy link

lewisje commented Jun 19, 2020

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

8 participants