检票口日期范围(从-到)验证
我有一个表单需要验证 DateFrom 和 DateTo。
我已经这样做了:
// start date
RequiredTextField<Date> startdateField =
new RequiredTextField<Date>("startDate", Date.class);
startdateField.add(new DatePicker(){
@Override
protected CharSequence getIconUrl() {
return RequestCycle.get().getUrlRenderer().renderContextPathRelativeUrl("/image/date-picker.png");
}
});
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,-1);
startdateField.add(DateValidator.minimum(cal.getTime()));
// end date
RequiredTextField<Date> enddateField = new RequiredTextField<Date>("endDate", Date.class);
enddateField.add(new DatePicker(){
@Override
protected CharSequence getIconUrl() {
return RequestCycle.get().getUrlRenderer().renderContextPathRelativeUrl("/image/date-picker.png");
}
});
// enddateField.add(DateValidator.minimum(startdateField.getModel().getObject()));
// this does not work . Form submitted ?
现在如何放置一个验证器,声明 endDate 必须等于或大于检票口中选定的开始日期?
有什么想法吗?帮助表示赞赏。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DateValidator.minimum(startdateField.getModel().getObject())
不起作用,因为在页面构建时,startdateField
的模型不保存用户提交,并且在验证时必须将其作为最低限度考虑。通常,如果您的验证涉及多个组件,则适合使用
IFormValidator
。它的validate()
方法将在成功调用每个依赖个体FormComponent。 validate()
,因此在继续验证它们之前,您可以保证每个依赖组件都有有效的单独输入。验证的一个重要方面是防止无效的用户输入到达组件的模型。因此,在验证时,模型尚未更新,而是
FormComponent.getModelObject()
,您必须使用FormComponent。 getInput()
或FormComponent。
。validate()
方法中的 getConvertedInput()请考虑到,如果
getDependentFormComponents()
无效(这意味着不可见、必需且没有输入,导致自定义个体失败验证等),FormValidator
将不会执行。您可能还会发现此信息很有用:验证相关字段
DateValidator.minimum(startdateField.getModel().getObject())
isn't working, because at page construction time,startdateField
's Model doesn't hold the value the user submits and which has to be taken into account as minimum at validation time.Usually, if your validation involves more than a single Component, it's appropriate to use an
IFormValidator
. Itsvalidate()
method will be invoked after successful invokation of each dependent individualFormComponent.validate()
, so you're guaranteed to have valid individual inputs on each dependent component before proceeding on to validate them altogether.One important aspect of validation is preventing invalid user input from reaching the Component's Models. Therefore, at validation time, Models will not be yet updated, and instead of
FormComponent.getModelObject()
, you'll have to useFormComponent.getInput()
orFormComponent.getConvertedInput()
in thevalidate()
method.Take into account that if any of the FormComponents in
getDependentFormComponents()
isn't valid (and that means being not visible, required and with no input, failing custom individual validations, etc), theFormValidator
will not execute.You may also find this information useful: Validating related fields