简体   繁体   English

在 Angular Reactive Forms 中使用跨字段验证时如何对所有表单控件强制执行必需的验证

[英]How to enforce required validation on all form controls when using cross-field validation with Angular Reactive Forms

I have a form where all fields are dropdowns and are all required.我有一个表单,其中所有字段都是下拉列表并且都是必需的。 There is also a condition where values between two different controls must be the same.还有一个条件是两个不同控件之间的值必须相同。 I have added a custom validator to utilize cross-field validation, but the problem I run into is if a user answers the questions with the special rules it will mark the entire form (FormGroup) valid even if there are still dropdowns that have not been touched yet.我添加了一个自定义验证器来利用跨字段验证,但我遇到的问题是,如果用户使用特殊规则回答问题,它会将整个表单 (FormGroup) 标记为有效,即使仍有下拉列表没有还摸着。

How can I maintain the cross-field validation in conjunction with requiring all fields to have a value?如何在要求所有字段都具有值的同时维护跨字段验证? All the examples I see are very simple 2 field forms that match password/confirm or date ranges.我看到的所有示例都是非常简单的 2 个字段表单,它们匹配密码/确认或日期范围。 My form has around 30 fields.我的表单有大约 30 个字段。

Code that creates the dynamic form:创建动态表单的代码:

      questions.forEach(question => {
        question.questions.forEach(child => {
          group[child.key] = new FormControl({ key: child.value } || '', Validators.required);
        });
      });

      return new FormGroup(group,
         [compareValues('26', '27', 'Questions 4 and 5 must have the same answer.')]);
    }

Custom Validator:自定义验证器:

import { AbstractControl, ValidationErrors, ValidatorFn } from "@angular/forms";

export function compareValues(val1: string, val2: string, message: string): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const mustMatch1 = control.get(val1);
    const mustMatch2 = control.get(val2);

    if (mustMatch1 && mustMatch2) {
      return mustMatch2.value === '' || mustMatch1.value.key === mustMatch2.value.key ? null : { mustMatch: true, message: message }
    }
  };
}

As I mentioned in my comment above it was a misunderstanding in the validation process due to a bug I had in my FormControl initialization.正如我在上面的评论中提到的,由于我在 FormControl 初始化中存在错误,这是验证过程中的误解。

It turns out the FormControl always had a valid value.结果证明 FormControl 总是有一个有效值。 Even if child.value was undefined or an empty string there would always be an object with a key being set which still is considered a valid entry with how I have this setup.即使child.value未定义或一个空字符串,总会有一个对象设置了一个键,它仍然被认为是我如何设置的有效条目。

Below is a change I made that cleaned up the validation and I have it working the way I intended.下面是我所做的一项更改,它清理了验证,并使其按我预期的方式工作。

Changing the previous:改变以前的:

  questions.forEach(question => {
    question.questions.forEach(child => {
      group[child.key] = new FormControl({ key: child.value } || '', Validators.required);
    });
  });

to the following:到以下几点:

  questions.forEach(question => {
    question.questions.forEach(child => {
      group[child.key] = new FormControl(child.value ? { key: child.value } : '', Validators.required);
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM