Reputation: 1396
I need to modify this method, so that when a user clicks on a button, all invalid form controls within the form group they are on, have their values reset or set to null.
The current method consists of this:
disableControl(group: FormGroup){
Object.keys(group.controls).forEach((key: string) => {
const abstractControl = group.get(key);
abstractControl.setValue(null)
abstractControl.disable();
})
}
When the user clicks on a button, any invalid form controls should be set to null or reset.
Upvotes: 1
Views: 37
Reputation: 73337
You can check if the form control is invalid
:
disableControl(group: FormGroup) {
Object.keys(group.controls).forEach((key: string) => {
const abstractControl: AbstractControl = group.get(key);
if (abstractControl.invalid) {
abstractControl.reset();
// .... or
// abstractControl.setValue(null);
}
})
}
Upvotes: 1