【发布时间】:2019-02-13 09:56:17
【问题描述】:
我正在尝试使用表单自定义验证器正确显示我的验证器,但我不知道如何调用它。我在 html 端尝试了 BroadcastForm.controls.errors.customTimeValidator() 但它不能正常工作。感谢您的帮助!
broadcast.component.ts
ngOnInit() {
this.BroadcastForm = this.fb.group({
datetime: [
datetime,
Validators.compose([Validators.required, this.customTimeValidator()]),
],
});
}
customTimeValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const minDate = new Date();
minDate.setSeconds(0);
this.minTime = minDate.getTime() + 59 * 1000;
const forbidden = control.value <= minDate;
return forbidden ? { forbiddenName: { value: control.value } } : null;
};
}
broadcast.component.html
<div class="validation-error" *ngIf="
BroadcastForm.controls.datetimeOption.value === 'false' &&
BroadcastForm.controls.errors.customTimeValidator()"> //how do I call customTimeValidator correctly?
Please select a future date/time
</div>
【问题讨论】:
标签: angular forms validation