troubleshooting Question

Angular. Problem with custom validator

Avatar of Steve Hougom
Steve HougomFlag for United States of America asked on
2 Comments1 Solution4 ViewsLast Modified:

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 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.

Steve Hougom
Developer II

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros