0

I want to change the validation for DOB field on create account page.

Currently the validation is in the format mm/dd/yyyy , I want to change the validation for that field to dd/mm/yyyy.

Can anybody tell me where is the validation code located. how to modify the validation rule for DOB field.

2 Answers 2

0

In this file :

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

functions is

public function getDateFormat()
    {
        $dateFormat = $this->_localeDate->getDateFormatWithLongYear();
        /** Escape RTL characters which are present in some locales and corrupt formatting */
        $escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);

        return $escapedDateFormat;
    }

Write its after plugin and change dates format what you want.

-1

You do not need to amend the code to acheive this you can update the formatting for dates in magento by the following method

  1. Log in the Magento 2 Admin
  2. Click on the Store section on Admin sidebar.
  3. Under the Setting, Go to Configuration
  4. Expand the Catalog option and choose the catalog option
  5. Set the date and time format to your prefered format and save

This should give you the result your after. you may need to redeploy your website

bin/magento setup:static-content:deploy 

Also you may need to flush the cache

bin/magento c:f

This should now give you the result your after

if the above does work the only other way would be to create an overwrite of Magento\Customer\Block\Widget\Dob and amend the getFieldHtml and getHtmlExtraParams functions to replace your with your specific date format

9
  • Did not work Still the validation rule is same <input type="text" name="dob" id="dob" value="" class="_has-datepicker mage-error" data-validate="{&quot;validate-date&quot;:{&quot;dateFormat&quot;:&quot;M\/d\/Y&quot;}}" autocomplete="off" aria-invalid="true" aria-describedby="dob-error">
    – Nafsss
    Commented Aug 30, 2019 at 6:16
  • have updated answer Commented Aug 30, 2019 at 7:20
  • @DavaGordan Did that too. public function getHtmlExtraParams() { $validators = []; if ($this->isRequired()) { $validators['required'] = true; } $validators['validate-date-au'] = "d/M/Y"; return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"'; } Still not working
    – Nafsss
    Commented Aug 30, 2019 at 7:29
  • did you also change getFieldHtml date_format Commented Aug 30, 2019 at 7:32
  • @DavaGordan. Yes, still getting error as Invalid date.
    – Nafsss
    Commented Aug 30, 2019 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.