angular - 如何获取验证器的错误类型
问题描述
我有一个简单的输入,我想在提交时获取错误类型。
formGroup: FormGroup = this.fb.group({
password: [
'',
[Validators.required, Validators.minLength(8), SpecialCharacters],
],
});
例如:
onSubmit(): void {
if (this.formGroup.invalid) {
//I get the type of error:
//number of characters less than 8
//you have not entered any special characters
}
}
解决方案
我会为特定的 formControl 尝试这样的事情
get getPassword(): AbstractControl {
return this.formGroup.get('password');
}
从 .html
<div *ngIf="getPassword.errors?.minLength">Minimum Length **</div>
<div *ngIf="getPassword.errors?.maxLength">Max Length ***</div>
推荐阅读
- javascript - CSS:不同分辨率的缩放问题
- javascript - 如何根据游戏分数有条件地显示图像?
- c# - 连接我的本地 .mdf 数据库(正确的代码)
- java - String.format() 用于同一行中的多个属性
- amp-html - 如何寻找特定时间然后开始动画?(去玩)
- google-cloud-platform - 限制对 Google Cloud Platform Console 的访问
- csv - 将数据集导入 Docker 下的 Clickhouse 时出现“异常:没有要插入的数据”
- javascript - 什么是首选:`toBe(null)` 或 `toEqual(null)`?
- java - SpringBoot 和 JPA 无法识别的 SQL 错误语法
- java - IgniteException:Spring XML 配置路径无效