2

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?!?

2
  • You can simply use javascript to compare date. If entered date is less than 1/1/1990 then return error, some thing like, if( startTime < endTime){ alert("start time is lesser"); }
    – Elavarasan
    Commented Sep 13, 2018 at 13:15
  • Thank you @Elavarasan for you comment, but the problem is not the algorithm for the check but how/where to add a new validation for that field. Commented Sep 13, 2018 at 15:44

1 Answer 1

2

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.

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.