简体   繁体   English

Angular 6反应性表单,FormArray异步验证器无法突出显示FormControl

[英]Angular 6 Reactive Form, FormArray Async Validator unable to highlight a FormControl

I have a FormArray in a FormGroup and each of the FormArray has multiple FormGroup's and can add them dynamically. 我在FormGroup中有一个FormArray,每个FormArray都有多个FormGroup,可以动态添加它们。

I have an Async Validator where it needs all the data in the FormGroup to validate the minimum payRate, currently have a dummy Async Validator which doesn't use all the FormGroup values but will need them to make the API call. 我有一个异步验证器,它需要FormGroup中的所有数据来验证最小的payRate,目前有一个虚拟的异步验证器,它不使用所有FormGroup值,但需要它们来进行API调用。

The issue comes because if I want all the data of the FormGroup, I need to add the Async Validator to the FormGroup and not the individual FormControl, to which i need to get ng-invalid to have a border-color: red. 出现问题是因为,如果我需要FormGroup的所有数据,则需要将异步验证器添加到FormGroup中,而不是将单个FormControl添加到单个FormControl中,我需要使ng- valid带有边框颜色:红色。

StqackBlitz link: https://stackblitz.com/edit/angular-vr4cya-b3r3od StqackBlitz链接: https ://stackblitz.com/edit/angular-vr4cya-b3r3od

addPay.push(
    new FormGroup({
      "payRate": new FormControl(assign.jobRate, Validators.required),
      "position": new FormControl(assign.position, Validators.required),
      "location": new FormControl(assign.location, Validators.required),
      "department": new FormControl(assign.department, Validators.required)
    },null,this.payRateValidator.bind(this))
  );

payRateValidator: payRateValidator:

payRateValidator(control: AbstractControl): {[key: string]: any} {
const payRate = control.get('payRate').value;
  const location = control.get('location').value;
  const department = control.get('department').value;

const promise = new Promise<any>((resolve, reject) => {
  setTimeout(() => {
    if (payRate <= 13) {
      resolve({'payRateisLess': true});
    } else {
      resolve(null);
    }
  }, 1500);
});
return promise;

} }

Can't have the Async Validator for the PayRate FormControl because of the Async Validator function cant access the values of other FormControl in the FormGroup. 由于异步验证器功能无法访问FormGroup中其他FormControl的值,因此不能具有PayRate FormControl的异步验证器。

addPay.push(
    new FormGroup({
      "payRate": new FormControl(assign.jobRate, Validators.required, this.payRateValidator.bind(this)),
      "position": new FormControl(assign.position, Validators.required),
      "location": new FormControl(assign.location, Validators.required),
      "department": new FormControl(assign.department, Validators.required)
    })
  );

Currently, when I click on +Additional Assignment button, by default, I'm getting Error text, "Please enter Pay Rate >13" , but this is not getting hidden when I type value greater than 13, because I need to enter all the inputs in the FormGroup since I have other validators for that are Required. 当前,当我单击+“其他分配”按钮时,默认情况下,我会收到错误消息“请输入薪水率> 13” ,但是当我键入大于13的值时,这并不会被隐藏,因为我需要输入所有FormGroup中的输入,因为我还有其他要求的验证器。

After I enter all the inputs in the current FormGroup the error message disappears. 在当前FormGroup中输入所有输入后,错误消息消失。

Is there any way to only show the error message, just for the Pay Rate Input tag when having Async Validator for the whole FormGroup???. 有什么方法只显示错误消息,仅在整个FormGroup具有异步验证器时才显示Pay Rate Input标签。

在此处输入图片说明

The problem that you want solved is not caused by the AsyncValidator . 您要解决的问题不是由AsyncValidator引起的。 The AsyncValidator is going to cause the entire form to be marked as invalid regardless of where it is placed because the payrate being invalid implies that the formGroup and the Form it is in is also invalid. 不论放置在哪里,AsyncValidator都将导致整个表单被标记为无效,因为Payrate无效表示formGroup及其所在的Form也无效。

To suppress the borders around invalid components you can use, 为了消除可以使用的无效组件周围的边界,

.cancel-border {
  border: none !important;
}

And apply that to any components that you want to not have an invalid border. 并将其应用于您希望没有无效边框的任何组件。 Or remove this bit entirely, if it is possible to edit that code: 或完全删除此位(如果可以编辑该代码):

div.ng-invalid {
  border: 1px solid red;
}

Having said that I have also moved your AsyncValidator function into the payRate control using a factory method: 话虽如此,我还使用工厂方法将AsyncValidator函数移至payRate控件中:

makeValidator(fg: FormGroup) {
return (control: AbstractControl) => {
  const payRate = fg.get('payRate').value;
  const location = fg.get('location').value;
  const department = fg.get('department').value;

  const promise = new Promise<any>((resolve, reject) => {
    setTimeout(() => {
      if (payRate <= 13) {
        resolve({ 'payRateisLess': true });
      } else {
        resolve(null);
      }
    }, 1500);
  });
  return promise;
}
}

and changed the way the validity is assessed. 并更改了评估有效性的方式。

<span [hidden]="assign.get('payRate').valid" ...>

There is a bit of code duplication at lines 54 and 70 that should be looked at and the validation message appearing every time a value is changed before disappearing. 在第54行和第70行有一些代码重复,应该看到,并且每次在消失之前更改值都会显示验证消息。 I think that can be fixed by using an observable instead of a promise. 我认为可以通过使用可观察的方法而不是承诺来解决。

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

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