Steve Hougom
asked on
Angular. Problem with custom validator
I have a field that requires conditional validation.
If a certain dropdown value is chosen then I need the field to be required. No problem with one field and custom validator. Unfortunately its email field and I have to add pattern and standard email validators too and this is causing me a nightmare to implement.
Currently the following works for required:
proofEmail: ['',conditionalValidator( (()=> this.isProofPDF === true),Validators.required)],
My conditionalValidator function here:
function conditionalValidator(condition: (()=> boolean), validator : ValidatorFn): ValidatorFn {
return (control : AbstractControl):{[key:string]:any} => {
if(! condition()){
return null;
}
return validator(control);
}
My html here:
<input class="form-control" formControlName='proofEmail' id="proof-email">
So Im simply trying to add validators.email and validators.pattern(mypattern)
here
but no matter what I try I cant get it right. When I add validators at runtime i have a different set of issues. Hope someone can help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER