我正在尝试在控件初始化后设置多个验证器,但似乎没有添加验证器。
代码:
this.infoForm.controls['email'].setValidators(this.DynamicValidation(validations));
DynamicValidation(list: Array<string>): Array<ValidatorFn> {
const validators: Array<ValidatorFn> = [];
list.forEach((validator: string) => {
if (validator === 'required') {
validators.push(Validators.required);
return;
}
// other validators implementation
}
}
一件令人讨厌的事情,有没有办法用这样的标志来设置自定义验证器名称,例如“ validators.push({isMatching:false}”。
答案 0 :(得分:0)
尝试一下:
this.infoForm.controls['email'].setValidators(this.DynamicValidation(validations));
DynamicValidation(list: Array<string>): Array<ValidatorFn> {
const validators: Array<ValidatorFn> = [];
list.forEach((validator: string) => {
if (validator === 'required') {
validators.push(Validators.required);
return validators;
}
// other validators implementation
}
}