How to trigger Form Validators in Angular2 How to trigger Form Validators in Angular2 angular angular

How to trigger Form Validators in Angular2


I don't know if you are still looking for an answer, so here is my suggestions:

Have a look at this: Angular 2 - AbstractControl

I think what you could do is following:

this.formGp.controls['checkboxFld'].valueChanges.observer({    next: (value) => {       this.formGp.controls['numberFld'].updateValueAndValidity();    }});

This should trigger and run the validators. Furthermore the state gets updated as well. Now you should be able to consult the checkbox value within your validator logic.

Hope this helps!

Validaton-Guide

FormControl Documentation


with my ControlGroup I do this because I have errors divs checking if touched

for (var i in this.form.controls) {  this.form.controls[i].markAsTouched();}

(this.form is my ControlGroup)


With the help of this blog

blog link

I have came across a solution with the combine of Nightking answer

Object.keys(this.orderForm.controls).forEach(field => {       const control = this.orderForm.get(field);       control.updateValueAndValidity();});

this.orderForm is the form group