Reactive Forms - mark fields as touched

后端 未结 19 1471
旧时难觅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:51

    This is my solution

          static markFormGroupTouched (FormControls: { [key: string]: AbstractControl } | AbstractControl[]): void {
            const markFormGroupTouchedRecursive = (controls: { [key: string]: AbstractControl } | AbstractControl[]): void => {
              _.forOwn(controls, (c, controlKey) => {
                if (c instanceof FormGroup || c instanceof FormArray) {
                  markFormGroupTouchedRecursive(c.controls);
                } else {
                  c.markAsTouched();
                }
              });
            };
            markFormGroupTouchedRecursive(FormControls);
          }
    
    0 讨论(0)
提交回复
热议问题