【问题标题】:Expected validator to return Promise or Observable async validator FormGroup angular 7预期验证器返回 Promise 或 Observable 异步验证器 FormGroup 角度 7
【发布时间】:2021-11-26 08:57:23
【问题描述】:

希望你是安全的。我正在使用 anuglar 7,我正在通过 aysnc 验证 api 调用验证模板名称是否存在于 db 中。我将异步验证器作为第三个参数传递,但是当我在输入字段上输入内容时出现错误。谁能帮帮我。我搜索了很多帖子,但没有一个能帮助我。谢谢你

问题 --> 当我输入一些值时,我收到“ERROR Error: Expected validator to return Promise or Observable.”

#下面是我的create-table.component.ts文件代码:

       ngOnInit() {
          this.getProducts();
   
    this.table = new FormGroup({

        tableName: new FormControl ('',
        Validators.required
        ,this.customTableNameValidator.bind(this)
         ),
    });
}

这里是异步函数

                  customTableNameValidator() {
                    return (control: AbstractControl) => {
                     return this.dataService.templateDuplicate(control.value).pipe(
                         map((res) => {
                         console.log("result : ", res["message"]);
                         return res["message"] ? { templateExist: true } : null;
                      })
                    );
                  };
                 }

这里是服务api调用代码。这里“res['message']”会返回布尔值

              templateDuplicate(tableName: string): Observable<any> {
                 return this.http
                     .post<any>(this.appBaseUrl + "validate-template-name/" + tableName, null)
                     .pipe(
                      map((res) => {
                      console.log("Res : ", res["message"]);
                       return res["message"];
                      })
                  );
               }

下面是Html文件元素标签

                 <input class="form-control" formControlName="tableName"  />

下面是 api 响应。我正在获取“消息”属性值并在上面的代码中返回它。

                  {
                    "statusMessage": null,
                    "filePath": null,
                    "message": true
                  }

【问题讨论】:

    标签: angular


    【解决方案1】:

    修改你的代码,看看它是否对你有帮助

      customTableNameValidator() {
        return (control: AbstractControl) => {
          return this.dataService.templateDuplicate(control.value).pipe(
            map((res:any) => {
              console.log("result : ", res["message"]);
              return res["message"] ? { templateExist: true } : null;
            })
          );
        };
      }
      ngOnInit() {
        this.table = new FormGroup({
          tableName: new FormControl ('',
           [ Validators.required, this.customTableNameValidator]
          ),
        });
      }
    

    【讨论】:

    • 嗨@Vue ang,感谢您调查此问题。我做了你建议的改变,但没有运气。我看到的是,当我更改输入值时,函数“customTableNameValidator()”被调用,但 api 调用未被调用。我也尝试将此函数作为第三个参数,因为它是异步验证,但这也不起作用。请帮忙
    • 异步验证器应该像这样使用ts tableName: new FormControl ('', null, [ Validators.required, this.customTableNameValidator] ),
    • 我尝试在数组中传递异步函数,但没有成功。你能建议任何其他选择吗?
    • Angular11正常,不好意思,可能你的版本太低了,请问需要异步检查什么
    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 2018-11-05
    • 2021-04-25
    • 2019-03-15
    • 2017-12-12
    • 2021-06-21
    • 2021-05-01
    相关资源
    最近更新 更多