angular - FormGroup.controls 在自定义验证器中未定义
问题描述
我有一个带有 html 页面的 Ionic 项目。该页面具有以下形式:
<form [formGroup]="configureLeagueForm">
<ion-item>
<ion-label position="floating">TEXT</ion-label>
<ion-input type="number" formControlName="roundsQuantity" ></ion-input>
</ion-item>
<ion-item *ngIf="!configureLeagueForm.controls.roundsQuantity.valid && (configureLeagueForm.controls.roundsQuantity.dirty || submitAttempt)">
<p [class.invalid]="!configureLeagueForm.controls.roundsQuantity.valid && (configureLeagueForm.controls.roundsQuantity.dirty || submitAttempt)" >TEXT IF ERROR .</p>
</ion-item>
</form>
和 .ts 文件
this.configureLeagueForm = this.formBuilder.group({
roundsQuantity: [1, Validators.compose([Validators.min(1), Validators.max(100), ConfigureChampionshipFormValidator.roundsQuantity])],
});
现在,我的验证器
export class ConfigureChampionshipFormValidator {
static roundsQuantity(group: FormGroup) : any{
var roundsQuantity = group.controls['roundsQuantity'].value;
if(String(roundsQuantity).trim() == ""){ // Just an example of validation
group.controls['roundsQuantity'].setErrors({"mandatory_error": true});
return { "mandatory_error": true };
} else{
return { "mandatory_error": false };
}
}
}
但在代码的这一步:
group.controls['roundsQuantity'].value
我有未定义的 group.controls。
这发生在我打开页面时。
解决方案
您的验证器设置在FormControl
( roundsQuantity
) 上。因此,它将作为参数接收的是表单控件。不是它的父表单组。
推荐阅读
- loops - 特定格式的 Ansible 调试消息
- javascript - 如何在 JavaScript 中反转函数?
- flutter - 在 Flutter 中,如何等待非未来函数完成?
- angular - 我如何在类型脚本或(.ts)中调用 scss 中的类
- reactjs - 使链接的子元素不路由
- rspec - Rspec 无法从 puppet 中找到 require 包,我可以在定义类型中跳过 require 吗?
- javascript - 使用包含数组值的过滤器数组
- flutter - Flutter矩阵手势获取大小和位置
- angular - 删除一个数组内的一个项目,该项目位于另一个数组内的角度?
- php - PHP - 如何在数组中搜索相同的值对