问题描述
date: [Validators.required,DateValidator.validateDate].
我正在使用moment.js处理日期,这是我的验证器:
static validateDate(fdValue: FormControl) {
const date = fdValue.value;
console.log(fdValue);
if(fdValue.errors!=null){
console.log("error different null");
}
if (date ==null && fdValue.errors!=null && fdValue.errors['matDatepickerParse']!=null){
return { pattern: {}};
}
}
通过此验证,我在输入13/10/2000
中添加了一个日期,它可以正常工作,但是我将这个日期设置为“ 10/0/2000”,所以它不起作用。
当我读到console.log(fdValue)
结果时:
errors:
matDatepickerParse:
text: "03/0/1977"
required: true
但是当我执行fdValue.errors!=null
时,如果fdValue.errors
为空,则不会输入这些内容。这是不可能的,因为它会向我输出错误的值,但是对于该程序为null。有人可以帮忙吗?
解决方法
如果日期不正确,需要打印一条消息,可以尝试:
static validateDate(fdValue: FormControl) {
if(fdValue.errors) {
console.log("error message goes here");
}
const date = fdValue.value;
if (date == null) {
return { pattern: {}};
}
return date;
}
我不确定您底部的if
语句在做什么,但是可以根据需要对其进行编辑-在这里,我这样做是为了让您的日期无效,它将返回{ {1}}