Angular form updateValueAndValidity of all children controls

后端 未结 5 656
攒了一身酷
攒了一身酷 2020-12-29 01:50

In my Angular 4 app, I have a form with several controls.

At some points I need to force the update of their validity, so I\'m doing:

this.form.get(\         


        
5条回答
  •  梦毁少年i
    2020-12-29 02:53

    I solved my issue, which was similar to yours, by recursing the controls and manually triggering the update. Probably this is not an optimal solution:

    private triggerValidation(control: AbstractControl) {
        if (control instanceof FormGroup) {
            const group = (control as FormGroup);
    
            for (const field in group.controls) {
                const c = group.controls[field];
    
                this.triggerValidation(c);
            }
        }
        else if (control instanceof FormArray) {
            const group = (control as FormArray);
    
            for (const field in group.controls) {
                const c = group.controls[field];
    
                this.triggerValidation(c);
            }
        }
    
        control.updateValueAndValidity({ onlySelf: false });
    }
    

提交回复
热议问题