问题描述
我有个约会(和momentjs)。日期必须为DD / MM / YYYY格式。
我的hmtl代码是:
BoxLayout:
orientation: 'horizontal'
spacing: 50
padding: 5
Button:
id: btn_aller
text: "{} - {} \n [size=12](cliquez pour plus d'information)[/size]".format(root.depart,root.destination)"
bold: True
我的控制权是:
<mat-form-field>
<input id="datanint" formControlName="dateBirth" matInput [matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker startView="multi-year" [startAt]="startDate"></mat-datepicker>
</mat-form-field>
这是我的验证者:
dateBirth: [DateValidator.validateDate],
问题是,当我放置一个这样的日期(13/13/2000)时,我希望在错误中(在表单控件中)有export class DateValidator implements Validators {
static validateDate(fdValue: AbstractControl) {
regexDate="...:";
console.log(fdValue);
if(fdValue != null && fdValue.value!= null && fdValue.value.match(regexDate) {
return { pattern: true };
}
}
,但不是这样,因为
pattern error
等于null(输入值为13/13/2000时)。我需要fdValue.value
(或我可以用来获取正确值的东西)的输入值必须相同,而且日期格式也不正确。
解决方法
在此Scenerio中,您无法获取fromControl的值,它将始终显示console.log(fdValue.value);
为null,因为可能是您在datepicker输入中传递了一些无效输入。
因此,当您将无效的值添加到日期选择器时,您的fdValue
为空,但仍然可以在error
对象中获取输入
static validateDate(fdValue: AbstractControl) {
const date = fdValue.value;
console.log('date: ',fdValue);
console.log('error: ',fdValue.errors?.matDatepickerParse?.text);
if (date ===null || date==='') return { requiredFromDate: true };
}
但是当您输入无效的日期输入时,您将得到fdValue.value
个错误,其中null
个对象包含该输入值
要检查代码,请检查angular-customvalidator-by-naib