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

asked Sep 12, 2018 at 9:26
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"); } 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.

answered Sep 20, 2019 at 12:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.