我希望自定义函数验证器在开始日期和结束日期的模糊上运行
组件.ts
Createform(){
this.studentForm = formBuilder.group({
StartDate: ['', Validators.required, customFunction(), updateOn: "blur" ],
EndDate: ['', null,customFunction(), updateOn: "blur" ],
}
自定义函数
customFunction(){
if (EndDate <= startDate) {
return of({ invalidDate: true });
}
}
return of(null);
};
}
它设置 CreateForm.controls['StartDate'].errors?。在开始日期运行时将 invalidDate 变为 true。即使我在开始日期上运行函数,是否可以在结束日期上设置此无效日期?
自定义函数运行正常,但是当它在 StartDate 运行时,它会在开始日期设置错误数组,但我希望在 EndDate 上设置错误,这可能吗?
在自定义函数中,您可以使用 setErrors 函数在不同字段上设置错误
自定义函数
customFunction(){
if (EndDate <= startDate) {
this.studentForm.form.controls['endDate'].setErrors({'invalidDate': true});;
}
}
return of(null);
};
}