简体   繁体   English

将自定义验证器添加到 Angular Reactive From 中的整个表单组

[英]Adding custom validator to whole form group in Angular Reactive From

I'm dynamically creating reactive forms in Angular based on JSON data received from API.我正在根据从 API 收到的 JSON 数据在 Angular 中动态创建反应式表单。 Sometimes form has only several formControls but sometimes there are a lot of form groups or formArrays with FormGroups.有时表单只有几个表单控件,但有时有很多表单组或带有表单组的表单数组。

I have a question about FormArray with several FormGroups.我有一个关于带有多个 FormGroup 的 FormArray 的问题。 In this FormGroups some FormControls can be required and some not.在这个 FormGroups 中,一些 FormControl 可能是必需的,有些则不是。 But the whole FormGroup is not required.但不需要整个 FormGroup。 Only if some FormControls in group are edited, after that whole form group must be valid (every required FormControl can't be empty).只有当组中的某些FormControls被编辑后,整个表单组必须有效(每个需要的FormControl不能为空)。

So my question is how to create custom validator for whole FormGroup, which will secure that if every FormControl in these concrete group will be empty, then this group will be valid.所以我的问题是如何为整个 FormGroup 创建自定义验证器,这将确保如果这些具体组中的每个 FormControl 都为空,则该组将有效。 But if for example one FormControl will be edited, then every required FormControl must be filled.但是,例如,如果要编辑一个 FormControl,则必须填写每个必需的 FormControl。

Thanks a lot for your ideas.非常感谢您的想法。

What about trying this simple solution试试这个简单的解决方案怎么样

this is my form init这是我的表单初始化

this.sponsorshipForm = this.fb.group(
  {
    startDate: ['', [Validators.required]],
    endDate: ['', [Validators.required]]
  },
  { validators: [this.sponsorshipDurationValidation] }
);

and this is my validator you can make whatever you need and customize it这是我的验证器,您可以制作任何您需要的东西并对其进行自定义

sponsorshipDurationValidation(form: FormGroup) {
   if (form.value.startDate && form.value.end) {
     console.log('form ', form, form.status);
   }
   if (something false happen or not valid value) {
     return { validUrl: true };
  }
  return null;
}

you add form group level validators like this using FormBuilder service:您可以使用 FormBuilder 服务添加这样的表单组级别验证器:

this.myFormGroup = this.formBuilder.group({ 
                     ... my group info ... 
                   }, {validators: [... validators ... ]);

the custom validator acts like any other, but the abstract control in this case is a FormGroup that can be treated like any other form group.自定义验证器的行为与任何其他验证器一样,但在这种情况下,抽象控件是一个 FormGroup,可以像对待任何其他表单组一样对待它。

something like:就像是:

function allOrNoneRequired(): ValidatorFn {
  return (ctrl: AbstractControl): ValidationErrors | null => {
    const fg = ctrl as FormGroup;
    const controls = Object.values(fg.controls);
    return (controls.every(fc => !fc.value) || controls.every(fc => !!fc.value))  
             ? null 
             : {allOrNoneRequired: true};
  };
}

then然后

this.myFormGroup = this.formBuilder.group({ 
                     ... my group info ... 
                   }, {validators: [allOrNoneRequired()]);

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

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