1//Validate all the form group
2this.form.updateValueAndValidity();
3
4//Validate only one form control
5this.form.controls['formControlName'].updateValueAndValidity();
1 this.myForm = this.formBuilder.group({
2 myControl1: this.defaultValue,
3 myControl2: this.defaultValue
4 });
5 debugger
6 this.myForm.setValidators(this.comparisonValidator())
7}
8
9 public comparisonValidator() : ValidatorFn{
10 return (group: FormGroup): ValidationErrors => {
11 const control1 = group.controls['myControl1'];
12 const control2 = group.controls['myControl2'];
13 if (control1.value !== control2.value) {
14 control2.setErrors({notEquivalent: true});
15 } else {
16 control2.setErrors(null);
17 }
18 return;
19 };