在 Ionic 的 Reactive Forms 中添加自定义验证时出错

Error occur while adding custom validation in Reactive Forms in Ionic

我一直在尝试在 ionic 的 Reactive Forms 中添加自定义验证,但是当我添加它时它会出错。

我的表单验证码:

this.loginForm = this.formBuilder.group({
  username: [
    "",
    [Validators.required, this.validationService.regExUsername],
  ],
  password: [
    "",
    [Validators.required, this.validationService.regExsPassword],
  ],
});

我的验证服务码:

regExUsername = "^[\w_.@][\w_.@-]*$";
regExPassword ="^[A-Za-z0-9[\]\!\@\#\^\(\)\+\=\è\é\à\ù\ò\_\-\*\$]*$";

我得到的错误:

       core.js:6241 ERROR Error: Uncaught (in promise): TypeError: v is not a function
       TypeError: v is not a function
       at forms.js:1599
       at Array.map (<anonymous>)
       at _executeValidators (forms.js:1595)
       at FormControl.validator (forms.js:1537)
       at FormControl._runValidator (forms.js:4347)
       at FormControl.updateValueAndValidity (forms.js:4308)
       at new FormControl (forms.js:4920)
       at FormBuilder.control (forms.js:9481)
       at FormBuilder._createControl (forms.js:9541)
       at forms.js:9520
       at resolvePromise (zone-evergreen.js:798)
       at resolvePromise (zone-evergreen.js:750)
       at zone-evergreen.js:860
       at ZoneDelegate.invokeTask (zone-evergreen.js:399)
       at Object.onInvokeTask (core.js:41645)
       at ZoneDelegate.invokeTask (zone-evergreen.js:398)
       at Zone.runTask (zone-evergreen.js:167)
       at drainMicroTaskQueue (zone-evergreen.js:569)  

我无法弄清楚问题出在哪里。如果有人知道,请告诉我。谢谢

可以通过修改用户名和密码的验证码来解决,如下:

  username: new FormControl(
    "",
    Validators.compose([
      Validators.required,
      Validators.pattern(this.validationService.regExUsername),
    ])
  ),
  password: new FormControl(
    "",
    Validators.compose([
      Validators.required,
      Validators.pattern(this.validationService.regExsPassword),
    ])