I am using the angular rc4 release to write an application where I have to impose few validation rules to form components like required
, minlength
along with some custom validator emailValidator
.
When I pass one built in and one custom validator to the Validators.compose
function, IDEs (both Webstorm & VS Code) display me compile time error messages like the one shown below:
However, you can see that in the above screenshot if both validators are built in, there is no error message.
The definition of my custom validator is given below:
static emailValidator(control: AbstractControl): {[key: string]: boolean} {
if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
return null;
} else {
return {'invalidEmailAddress': true };
}
}
I solved the problem by replacing the type of control
parameter from AbstractControl
to Control
as shown below:
static emailValidator(control: Control): {[key: string]: boolean} {
if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
return null;
} else {
return {'invalidEmailAddress': true };
}
}
Control
class extends from AbstractControl
. I still don't understand why I get this error message when I use FormControl
or AbstractControl
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With