javascript - 如何显示来自 AsyncValidatorFn 的消息

标签 javascript angular angular7

我正在实现一个 AsyncValidatorFn。

服务:

public verifyExistingRegisterScheduled(workschedule: WorkSchedule): Observable<Result<any>> {
return this.dataService.post<Result<any>>('/workschedule/existing-register-scheduled', workschedule);

}

组件:

createForm() {
this.scheduleForm = this.formBuilder.group({
  searchText: [this.searchText],
  codEnd: [this.workschedule.codEnd, Validators.required],
  dataInicio: [this.workschedule.dataInicio, Validators.required],
  dataFim: [this.workschedule.dataFim, Validators.required],
  periodo: [this.workschedule.periodo, Validators.required],
  justificativa: [this.workschedule.justificativa, Validators.required],
  totalColaboradores: [this.workschedule.totalColaboradores],
  totalTerceiros: [this.workschedule.totalTerceiros]
},
  {
    validator: [dateLessThanValidator, existingRegisterScheduledValidator(this.workScheduleService, this.workschedule)]
  });

}

验证器:

export function existingRegisterScheduledValidator(workScheduleService: WorkscheduleService, workschedule: WorkSchedule): AsyncValidatorFn {
return (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => {
    return workScheduleService.verifyExistingRegisterScheduled(workschedule).pipe(map(result => {
        debugger;
        return (result.content && result.content.length > 0) ? { 'registerExists': true } : null;
    }));
}

};

HTML

<div *ngIf="scheduleForm.errors && scheduleForm.errors['registerExists']" class="alert alert-danger col-sm-12">Message.</div>

这个想法是,如果返回值,则错误可能是真的。

不工作。我做错了什么?

最佳答案

您错误配置了asyncValidator。请参阅docs供使用

createForm() {
this.scheduleForm = this.formBuilder.group({
  searchText: [this.searchText],
  codEnd: [this.workschedule.codEnd, Validators.required],
  dataInicio: [this.workschedule.dataInicio, Validators.required],
  dataFim: [this.workschedule.dataFim, Validators.required],
  periodo: [this.workschedule.periodo, Validators.required],
  justificativa: [this.workschedule.justificativa, Validators.required],
  totalColaboradores: [this.workschedule.totalColaboradores],
  totalTerceiros: [this.workschedule.totalTerceiros]
},
  {
    validators: [dateLessThanValidator], 
    asyncValidators :[existingRegisterScheduledValidator(this.workScheduleService, this.workschedule)]
  });

关于javascript - 如何显示来自 AsyncValidatorFn 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793481/

相关文章:

javascript - "done"body向上滚动时Pace Js

html - 如何使内联 HTML 样式覆盖 CSS 样式?

javascript - 根据时间在脚本标签和样式表中附加动态版本(变量)

javascript - JSON编码的数组,如何在jQuery中使用

Java HtmlUnit 网页抓取器 newPage 不可访问

angular - angular 2中的多个自定义验证

javascript - 如何使用 filter() 从 Id 列表中查找多个项目?

具有命名路由器导出 : ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL 段的 Angular 辅助路由

twitter-bootstrap - Bootstrap 4.2.1 - 如何覆盖按钮轮廓?

javascript - 这些双括号在 JS 中有什么作用?