【问题标题】:How to implement Custom Async Validator in Angular4如何在 Angular4 中实现自定义异步验证器
【发布时间】:2017-09-08 01:20:29
【问题描述】:

我在我的项目中应用 Angular 4,但在使用 HTTP 请求进行自定义验证时遇到问题。

我也试过这个:How to implement Custom Async Validator in Angular2

但它在我的项目中不起作用。

这是我到目前为止所做的:

验证投标:

    userName: ['', [Validators.required, Validators.minLength(3), this.validateUserName.bind(this)]]

值改变事件:

    let userNameControl = this.individualForm.get('userName');
userNameControl.valueChanges.subscribe(value => {
    this.setErrorMessagesForUserNameControl(userNameControl)
  }
);

验证函数:

validateUserName(control: FormControl): Promise<any> {
let promise = new Promise<any>(
  (resolve, reject) => {
    if (this.oldUserNameForCheck != undefined) {
      this._individualUpdateService.isUserNameExisting(this.oldUserNameForCheck, control.value).subscribe(
        (res) => {
          if (res.isUserNameExisting) {
            console.log("existing");
            resolve({'existing': true});
          } else {
            console.log("NOT existing");
            resolve(null);
          }
        },
        (error) => {
          console.log(error);
        }
      );
    } else {
      resolve(null);
    }
  }
);
return promise;
}

Error Messages

我只是尝试通过将用户名发送到后端来验证用户名。

Here's the logs

如您所见,有一条“required”、“minlength”消息。那些工作正常。但是自定义验证的消息不是很清楚。

【问题讨论】:

    标签: validation angular asynchronous


    【解决方案1】:

    异步验证器应该在下一个参数中

    userName: ['', 
        [ Validators.required, Validators.minLength(3) ], 
        [ this.validateUserName.bind(this) ] 
    ]
    

    另外,最好有一个工厂方法,它具有创建验证器所需的依赖项,而不是使用 'bind(this)'

    userNameValidator(originalUsername, individualUpdateService) {
        return (control: FormControl): Promise<any> => {
            return new Promise<any>((resolve, reject) => {
                if (originalUsername != undefined) {
                    individualUpdateService.isUserNameExisting(originalUsername, control.value).subscribe(
                        (res) => {
                            if (res.isUserNameExisting) {
                                console.log("existing");
                                resolve({ 'existing': true });
                            } else {
                                console.log("NOT existing");
                                resolve(null);
                            }
                        },
                        (error) => {
                            console.log(error);
                        }
                    );
                } else {
                    resolve(null);
                }
            })
        }
    }
    
    userName: ['', 
        [ Validators.required, Validators.minLength(3) ], 
        [ this.userNameValidator(this.oldUserNameForCheck, this._individualUpdateService) ] 
    ]
    

    【讨论】:

    • 感谢您的帮助,但它不起作用。这是控制台上打印的日志:dropbox.com/s/19acvswgf1951s9/2017-04-19_160732.jpg?dl=0
    • 我这样做了:userName: ['', [Validators.required, Validators.minLength(3)], [this.validateUserName.bind(this)]],
    • @HowardHo 你的简单评论拯救了我的一天,我只是忘记了我们可以将它绑定到验证。
    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2023-03-12
    • 2020-06-19
    相关资源
    最近更新 更多