我有一个反应形式,表格使用ngFor嵌套。我需要根据银行名称验证银行帐号,每个表单的银行帐号可能不同。我遇到的问题似乎只验证一个表单实例的银行帐号。
自定义验证器
ValidateBankAccountNumber(control: AbstractControl) {
if (this.ddGroup.controls) {
const bankName = this.ddGroup.controls['bank'].value;
if (bankName === "") {
return { noBank: true };
}
const bankAcNo: string = control.value;
if (bankName === 'MBC-01' && bankAcNo.length !== 12) {
return { wrongMCBAcc: true };
}
}
return null;
}
根据付款方式,验证人将按以下方式更新
modeControlChanges$.subscribe(mode => {
if (mode === 'DD') {
//loop through each control of the formgroup and set validators accordingly
Object.keys(formDdGroup.controls).forEach(key => {
if (key === "bankNo" && modeTypeControl.value === "AD") {
formDdGroup.controls[key].setValidators([Validators.required, this.ValidateBankAccountNumber.bind(this)]);
}
formDdGroup.controls[key].updateValueAndValidity();
});
}
})