Set invalid invalid form control in angular2

Is it possible to set invalid form control in angular2? (used form builder)

eg. I have form-> exampleFrom and field-> exampleControl

I tried this, it failed:

this.exampleFrom.controls['exampleControl'].invalid 
+6
source share
2 answers

Try

let control = this.exampleFrom.controls['exampleControl'];
control.setErrors({backend: {someProp: "Backend message"}});
let message = control.errors['backend'].someProp;
+6
source

try this code to print a message

 <div style='color:red' *ngIf="!exampleFrom.exampleControl.valid">
                        //set here your custom message
          </div>
+2
source

Source: https://habr.com/ru/post/1017077/


All Articles