【问题标题】:Error occur while adding custom validation in Reactive Forms in Ionic在 Ionic 的反应式表单中添加自定义验证时发生错误
【发布时间】:2020-09-29 23:01:40
【问题描述】:

我一直在尝试在 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)  

我无法弄清楚是什么问题。如果有人知道,请告诉我。谢谢

【问题讨论】:

    标签: ionic-framework ionic4 angular-reactive-forms


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 2021-10-16
      • 2020-01-13
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多