【问题标题】:Angular reactive forms using minlength only shows error when length is 0使用 minlength 的角度反应形式仅在长度为 0 时显示错误
【发布时间】:2018-03-22 07:31:50
【问题描述】:

即使我将 minlength 设置为 5,它也只会在长度为 0 时给我一个错误。

这是我制作的一个 plunker,证明了这一点:

https://plnkr.co/edit/bJ9zeArGykATtRGh3Zev?p=preview

这就是我设置验证的方式:

  formGroup.sensitivity = 
   this.formBuilder.control(this.reportModel.sensitivity, [Validators.required, 
   Validators.minLength(5)]);

【问题讨论】:

  • 嘿嘿,对不起 :) 现在就在那里
  • 对不起,打错了人...
  • 呵呵现在是正确的!很抱歉:)
  • 你想要一个数字类型的输入?
  • 因为如果你使用文本输入,它工作得很好! plnkr.co/edit/94zJkOySH39CnZOekst6?p=preview

标签: angular validation reactive-forms


【解决方案1】:

对于数字,如果要检查必须至少为 5 的数字,则应使用 Validators.min() 而不是 minLength()。minLength() 用于文本中的字符数。

formGroup.sensitivity = 
   this.formBuilder.control(this.reportModel.sensitivity, [Validators.required, 
   Validators.min(5)]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 2022-01-09
    • 2018-12-06
    • 2019-01-19
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多