How to check if a FormControl has a `min` validator
Unfortunately `hasValidator` does not work well with validators created
dynamically like `Validator.min` but there is a solution that allows getting information about min value anyway.
You can view the source code for this project by following this link: GitHub
Problem with hasValidator
Let’s create a simple FormControl with min validator.
To check if the control has some validator we can use the hasValidator method
but it does not work correctly for Validators.min(3) because calling Validators.min(3)
creates another reference and the implementation of hasValidator check validators by reference.
Checking if a control has a min validator without hasValidator
To check if the control has a min validator we can use knowledge about numbers.
We can get all control’s validators and check if we get the min error for -Infinity.
Because nothing can be lesser than -Infinity we are sure that we cause an error if the validator contains min validator.
Getting min value from the validator
To get information about the min value from the validator first we need to cause an error.
In the error details, we have information about the min value and actual value of a control.
We have to write errors['min']['min'] because first min is the error key and the second min is the value passed as the argument of Validators.min function.
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.