I'm using Angular 4.4.3 reactive forms to implement custom validation for a group of controls in a form. The method AbstractControl.setErrors, according to docs updates the errors property of the AbstractControl, upon which it's invoked, updates the status of its parent, but not the parents errors property. I wanted to set the errors property on a FormGroup instance, so I use the setErrors inherited by the FormGroup. However, it does not update errors as expected.
Following is my sample code: Trying it on FormControl instances, does update their errors as well as their parents' validity status (not the parents errors though!):
let myFormGroup
= this._formBuilder
.group({
ctrl1: [null],
ctrl2: [null]
},
{
validator: (fg: FormGroup) => {
let ctrl1 = fg.get('ctrl1'),
ctrl2 = fg.get('ctrl2'),
ctrl1Empty = !ctrl1.value,
ctrl2Empty = !ctrl2.value;
//Successfully sets ctrl1.errors and fg.status, but not fg.errors
if (ctrl1empty)
ctrl1.setErrors({ctrl1required: true});
//Successfully sets ctrl2.errors and fg.status, but not fg.errors
if (ctrl2Empty)
ctrl2.setErrors({ctrl2required: true});
//Doesn't work, doesn't update fg.errors
if (ctrl1Empty && ctrl2Empty)
fg.setErrors({required: true});
}
})
Any idea why?