Let's say I've got this custom validator, I was wondering whether there is a way to use the object we return when the validation fails.
export class PasswordValidator{
private static min:number = 3;
private static max:number = 32;
static setMin(n:number){ PasswordValidator.min = n; }
static setMax(n:number){ PasswordValidator.max = n; }
static validatePw(ctrl:FormControl){
let err:number = 0;
let pw = ctrl.value;
err += pw.length < PasswordValidator.min ? 1 : 0;
err += pw.length > PasswordValidator.max ? 1 : 0;
err += (/([a-z\-]*[A-Z].*[\d][a-z\-]*)|([a-z\-]*[\d].*[A-Z][a-z\-]*)/).test(pw) ? 0 : 1;
return err > 0 ? {validatePw:"it is a valid goHenry password"} : null;
}
}
For example doing (in the main component):
errortMsg = this.myform.controls['password'].validatePw
because I don't see the point to return an object if we I can't use it. Any suggestion?
You can get it from the errors
property of the control:
errortMsg = this.myform.controls['password'].errors.validatePw