0

In a component with an input, I pass an array of such objects for validation:

export class ValidatorItem {
    validator: ValidatorFn;
    errorText: string;
}

in component:

 ngOnChanges(changes: SimpleChanges): void {
    if ('validators' in changes) {
      this.formModel = this.formBuilder.group({
        value: ['', Validators.compose(this.validators.map(v => v.validator))],
      });
    }
 }

that is, I need to get an errorText message depending on which validator fell. How can i do this?

evrei666
  • 83
  • 4
  • Please go through this link. [Validations](https://stackoverflow.com/questions/52771445/best-way-to-show-error-messages-for-angular-reactive-forms-one-formcontrol-mult) – Priti Dec 28 '22 at 07:32
  • I had issues before reassigning a "formModel" - if that's what you're doing - as some of the components didn't update with the form control. You might want to look at patching the existing form with the changes – Drenai Dec 28 '22 at 08:30
  • 1
    A validator is a function that return null (if all is Ok) or, generally, an object if there're an error. you simply ask about one property of this object. e.g. Validators.email return an object `{email:'error'}`, Validators.required return an object `{required:true}`and so on. so you ask about `control.errors?.email` or about `control.errors?.required`. – Eliseo Dec 28 '22 at 10:34

0 Answers0