Avatar of Steve Hougom
Steve Hougom
Flag for United States of America 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)],

Open in new window

My conditionalValidator function here:

function conditionalValidator(condition: (()=> boolean), validator : ValidatorFn): ValidatorFn {
  return (control : AbstractControl):{[key:string]:any} => {
    if(! condition()){       return null;     }     return validator(control);   }

Open in new window

My html here:

 <input class="form-control" formControlName='proofEmail' id="proof-email">

Open in new window

So Im simply trying to add validators.email and validators.pattern(mypattern) 


 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.


Avatar of undefined
Last Comment
Steve Hougom

8/22/2022 - Mon
Steve Hougom

If i do this it errors out.

Steve Hougom

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy