I need to iterate through all controls in Formbuilder, and children formbuilders to clear validators and updateValueAndValidity. How can I update answer from here to do so ?
Angular 2: Iterate over reactive form controls
Object.keys(this.form.controls).forEach(key => {
this.form.controls[key].clearValidators();
this.form.controls[key].updateValueAndValidity();
});
This form here has formbuilders within formbuilders, etc. The answer above only affects top level children, not subchildren, etc
this.editSharedForm = this.formBuilder.group({
'name': [null, [Validators.maxLength(50)]],
'streetAddress': [null, [Validators.maxLength(50)]],
'city': [null, [Validators.required,Validators.maxLength(200)]],
'zipcode': [null, [Validators.maxLength(10)]],
'phoneNumber': [null, [Validators.maxLength(50)]],
'emailAddress': [null, [Validators.maxLength(50), Validators.email]],
'addressChangeReason': this.formBuilder.group({
'addressChangeReasonId': [null, [Validators.required, Validators.min(1)]],
'addressChangeReasonCode': [null, [Validators.required, Validators.maxLength(50)]],
'addressChangeReasonDescription': [null, [Validators.required, Validators.maxLength(255)]]
}),
'addressSource': this.formBuilder.group({
'sourceOfAddressId': [null, [Validators.required, validatorDropdown]],
'sourceOfAddressCode': [null, [Validators.required, Validators.maxLength(50)]],
'sourceOfAddressDescription': [Validators.required, Validators.maxLength(255)]]
})
});
}
2) Additionally, how can I just target 1 subchild (eg addressChange Reason) if needed ? Following is not working
Object.keys(this.editSharedForm.get('addressChangeReason').controls).forEach(key => {
this.editSharedForm.controls[key].clearValidators();
this.editSharedForm.controls[key].updateValueAndValidity();
});
Property 'controls' does not exist on type 'AbstractControl'