我想为Ionic ion-datetime
元素添加一些验证,我有这个代码。
<ion-item>
<ion-label>Birth of Date</ion-label>
<ion-datetime displayFormat="MM/YYYY" pickerFormat="MMMM YYYY" min="1940" max="{{lastYear | date:'yyyy'}}">
</ion-datetime>
</ion-item>
而且我希望在一个表单中设置此字段必须是必填字段。我希望这个最大年份应该比当年低16年(-16)
到目前为止我一直在尝试这个但是没有用
lastYear = null;
this.lastYear = new Date('yyyy');
我试图从这里扣除-16但是给我错误
无法读取属性'year'的null
有人可以给我正确的方法来进行这2次验证。谢谢
将表单组添加到该组件,并在该表单组中声明日期和时间的表单控件名称,并将其声明为必需的名称,如下例所示
public holidayForm: FormGroup;
constructor public fb: FormBuilder){
this.holidayForm = fb.group({
'holiday_date': [null, Validators.compose([Validators.required])],
});
}
当你提交表格检查天气时,表格是否有效,如果有效,则提交,如果没有则在你的应用程序中显示所需的消息。
2.i希望这个最大年份应该比当年低16年(-16)
this.currentTime = new Date();
this.year = this.currentTime.getFullYear();
this.year = this.year - 16;
试着用这个希望这对你有所帮助