Angular form async validator firing but not updating form validity

This is my first time trying an async form validator. It is attached at to my form’s asyncValidators, it’s firing appropriately, the correct value (object or null) is being returned … but the form remains invalid and the state stuck in PENDING. The errors property on all formgroups and formcontrols is null.

If I remove the async validator, the issue goes away, so it is 100% the validator.

This is an old component that someone injected ChangeDetectorRef into, but is not an onPush component. I have tried using the cd to mark for check and explicitly detectChanges().

Can someone tell me what I’m doing wrong?

Validator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private validateV2Applications$(): AsyncValidatorFn {
return (): Observable<ValidationErrors> => {
console.log("called");
return this.applyChargesService.rowInfo$.pipe(
map((rowInfo) => {
console.log('mapped');
if (this.useApplyToInstallments && rowInfo.invalidRows.length > 0) {
console.log('async validator is invalid!');
return { applicationErrors: 'there are application errors' };
}
return null;
}),
);
};
}
</code>
<code>private validateV2Applications$(): AsyncValidatorFn { return (): Observable<ValidationErrors> => { console.log("called"); return this.applyChargesService.rowInfo$.pipe( map((rowInfo) => { console.log('mapped'); if (this.useApplyToInstallments && rowInfo.invalidRows.length > 0) { console.log('async validator is invalid!'); return { applicationErrors: 'there are application errors' }; } return null; }), ); }; } </code>
private validateV2Applications$(): AsyncValidatorFn {
    return (): Observable<ValidationErrors> => {
      console.log("called");
      return this.applyChargesService.rowInfo$.pipe(
        map((rowInfo) => {
          console.log('mapped');
          if (this.useApplyToInstallments && rowInfo.invalidRows.length > 0) {
            console.log('async validator is invalid!');
            return { applicationErrors: 'there are application errors' };
          }

          return null;
        }),
      );
    };
  }

Form creation code (wasn’t sure how to add as part of formBuilder so adding after-the-fact):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// fb = FormBuilder
this.creditForm = this.fb.group(
{ /* form controls */ },
{
validator: [this.validatecredit(), this.validateApplicationTotal()],
},
);
this.creditForm.setAsyncValidators([this.validateV2Applications$()]);
</code>
<code>// fb = FormBuilder this.creditForm = this.fb.group( { /* form controls */ }, { validator: [this.validatecredit(), this.validateApplicationTotal()], }, ); this.creditForm.setAsyncValidators([this.validateV2Applications$()]); </code>
// fb = FormBuilder
this.creditForm = this.fb.group(
  { /* form controls */ },
  {
    validator: [this.validatecredit(), this.validateApplicationTotal()],
  },
);

this.creditForm.setAsyncValidators([this.validateV2Applications$()]);

EDIT for @naren murali, the code for rowInfo$. The rowInfo$ observable works perfectly fine for every other use case. This code is located in a service, and another component pushes rowInfo to listeners. That “other component” is loaded and pushing values, which I can verify with the log statements.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private rowInfoSource = new BehaviorSubject<ApplyChargesRowInfo>({
selectedRows: [],
invalidRows: [],
});
public rowInfo$ = this.rowInfoSource.asObservable();
public setRowInfo(data: ApplyChargesRowInfo): void {
this.rowInfoSource.next(data);
}
</code>
<code>private rowInfoSource = new BehaviorSubject<ApplyChargesRowInfo>({ selectedRows: [], invalidRows: [], }); public rowInfo$ = this.rowInfoSource.asObservable(); public setRowInfo(data: ApplyChargesRowInfo): void { this.rowInfoSource.next(data); } </code>
private rowInfoSource = new BehaviorSubject<ApplyChargesRowInfo>({
  selectedRows: [],
  invalidRows: [],
});
public rowInfo$ = this.rowInfoSource.asObservable();

public setRowInfo(data: ApplyChargesRowInfo): void {
  this.rowInfoSource.next(data);
}

1

After analyzing your shared code, I don’t see any tells that can explain your issue. I suspect a timing issue because you added your async validators after creating the form group. I can help you with one part of your question, and we might get lucky and this solves the problem.

You can register async validators with form builder like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// fb = FormBuilder
this.creditForm = this.fb.group(
{ /* form controls */
myControl: [
'',
undefined, /* no validators, or your existing validators here */
[this.validateV2Applications$] /* async validators */
]
},
{
validator: [this.validatecredit(), this.validateApplicationTotal()],
},
);
</code>
<code>// fb = FormBuilder this.creditForm = this.fb.group( { /* form controls */ myControl: [ '', undefined, /* no validators, or your existing validators here */ [this.validateV2Applications$] /* async validators */ ] }, { validator: [this.validatecredit(), this.validateApplicationTotal()], }, ); </code>
// fb = FormBuilder
this.creditForm = this.fb.group(
  { /* form controls */ 
    myControl: [
      '', 
      undefined, /* no validators, or your existing validators here */ 
      [this.validateV2Applications$] /* async validators */
    ] 
  },
  {
    validator: [this.validatecredit(), this.validateApplicationTotal()],
  },
);

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật