How to display max length based on FormControl’s validator
Unfortunately `hasValidator` does not work well with validators created dynamically
like `Validator.maxLength` but there is a solution
that allows getting information about maxLength value anyway.
Take a look at the title hint. Try to provide a title longer than 15 chars.
Problem with hasValidator
Let’s create a simple FormControl with maxLength validator.
To check if the control has some validator we can use the hasValidator method
but it does not work correctly for Validators.maxLength(15) because
calling Validators.maxLength(15) creates another reference
and the implementation of hasValidator check validators by reference.
Checking if a control has a maxLength validator without hasValidator
To check if the control has a maxLength validator let’s
first take a look into maxLength implementation.
As you can see validator checks if a control’s value has a valid length (if it is an object with a length property) and then compares this length with maxLength argument.
We can use this knowledge to create a fake object with a length property.
We can get all control’s validators and check if we get the maxLength error for { length: Infinity }.
Because nothing can be bigger than Infinity we are sure that we cause an error if the validator contains maxLength validator.
Getting maxLength value from the validator
To get information about the maxLength value from the validator first we need to cause an error.
In the error details, we have information about the maxLength value and actual value of a control.
Do you like the content?
Your support helps me continue my work. Please consider making a donation.
Donations are accepted through PayPal or Stripe. You do not need a account to donate. All major credit cards are
accepted.