我有一个用户需要输入经度的输入。我希望当用户键入NaN或Not-Precise-Enough值时,能够显示不同的错误信息。我使用FormControl.hasError('ValidatorsName')通过验证来获取错误,但似乎无法区分这些模式。
模板:
<mat-form-field class="form-field">
<input matInput placeholder="Logitude" [formControl]="longitude">
<mat-error *ngIf="longitude.hasError('pattern')">
Longitude must be <strong>a number</strong>
</mat-error>
<!-- I want to be able to check patter2 for example -->
<mat-error *ngIf="longitude.hasError('pattern')">
Longitude must have <strong>a minimum of 5 decimal numbers</strong>
</mat-error>
</mat-form-field>和我的Angular代码:
this.longitude = new FormControl(this.attraction.longitude, [
// is valid number
Validators.pattern(new RegExp('(\d{1,3})([.|,]{,1})(\d+))','gi')),
// is precise enough
Validators.pattern(new RegExp('(\-{0,1})(\d{1,3})([.|,]{1})(\d{5,13})','i'))
]);有没有办法给这些模式一个标识符?如果有任何帮助,我将不胜感激。
发布于 2018-02-18 01:35:19
不幸的是,无法为验证器提供自定义标识符。
更新17.02.2018 21:00
正如Dawid Zbinski在评论中提到的具有相同错误的多个错误(例如:'pattern')获得覆盖,我更新了我的答案。
创建一个自定义验证器,将regex和预定义的错误作为参数传递给该验证器:
regexValidator(regex: RegExp, error: ValidationErrors): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
if (!control.value) {
return null;
}
const valid = regex.test(control.value);
return valid ? null : error;
};
}
并像这样使用它:
this.longitude = new FormControl('', [
this.regexValidator(new RegExp('^[0-9]+$'), {'number': ''}),
this.regexValidator(new RegExp('^.{5,}$'), {'precision': ''})
]); <mat-form-field class="form-field">
<input matInput placeholder="Logitude" [formControl]="longitude">
<mat-error *ngIf="longitude.hasError('number')">
Longitude must be <strong>a number</strong>
</mat-error>
<!-- I want to be able to check patter2 for example -->
<mat-error *ngIf="longitude.hasError('precision')">
Longitude must have <strong>a minimum of 5 decimal numbers</strong>
</mat-error>
</mat-form-field>
我还更新了stackblitz演示:https://stackblitz.com/edit/angular-dvwcj3?file=app%2Fhello.component.ts
的老答案:
但是PatternValidators返回的是唯一的ValidatonErrorObjects。
当您从official angular repo上的模式验证器签出源代码时,您可以看到它们总是在error对象中返回正则表达式。
return (control: AbstractControl):ValidationErrors | null => { if (isEmptyInputValue(control.value)) { return null;//不验证空值以允许可选控件}常量值: string = control.value;return regex.test(control.value)?null:{'pattern':{'requiredPattern':regexStr,'actualValue':value}};};
记住这一点,您可以很容易地在component.ts文件中创建两个getter方法。并且它们在两个正则表达式之间是不同的。在本例中,它们只是检查唯一的子字符串是否与正则表达式匹配。可以肯定的是,它们是处理这个问题的其他方法。
get numberError() {
if (this.longitude && this.longitude.hasError('pattern')) {
return this.longitude.getError('pattern').requiredPattern.indexOf('(\d+)') !== -1
}
return false;
}
get preciseError() {
if (this.longitude && this.longitude.hasError('pattern')) {
return this.longitude.getError('pattern').requiredPattern.indexOf('(\-{0,1})') !== -1
}
return false;
}
发布于 2022-01-08 23:05:27
直到我将error对象的值从''更改为true,SplitterAlex's solution才对我起作用
this.longitude = new FormControl('', [
this.regexValidator(new RegExp('^[0-9]+$'), {'number': true}),
this.regexValidator(new RegExp('^.{5,}$'), {'precision': true})
]);https://stackoverflow.com/questions/48843538
复制相似问题