Reactive Forms - mark fields as touched

后端 未结 19 1529
旧时难觅i
旧时难觅i 2020-12-04 23:15

I am having trouble finding out how to mark all form\'s fields as touched. The main problem is that if I do not touch fields and try to submit form - validation error in not

19条回答
  •  一生所求
    2020-12-04 23:45

        /**
        * Marks as a touched
        * @param { FormGroup } formGroup
        *
        * @return {void}
        */
        markFormGroupTouched(formGroup: FormGroup) {
            Object.values(formGroup.controls).forEach((control: any) => {
    
                if (control instanceof FormControl) {
                    control.markAsTouched();
                    control.updateValueAndValidity();
    
                } else if (control instanceof FormGroup) {
                    this.markFormGroupTouched(control);
                }
            });
        }
    

提交回复
热议问题