Magento – Magento2: Add date-from validation for dob (date of birth) field in customer creation

customerdateform-validationmagento2ui-form

I want to limit the dob field in account creation form to be not less than 01/01/1900.

I thought 2 way:
– limit the calendar widget (but maybe someone could input the date directly)
– add a validation to the dob field

The first way seems not to be secure, so i decide to follow the second one.

I found there is a validation class "validate-date-range" which seems to do what i need, but i can't find how to use it (how can i pass the initial date parameter?)

Moreover, I don't know how to add the validation to the dob field in the account create form.

I try to modify the file
vendor/magento/module-customer/view/base/ui_component/customer_form.xml
but i can't see change in the frontend code.

Any help?!?

Best Answer

Then you need to override the block file and make changes for the validation.

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

You can add your custom validation in above function.