检票口日期范围(从-到)验证 [英] wicket date range (From-To) validation
问题描述
我有一个需要验证DateFrom和DateTo的表单.
I have a form where I need to validate DateFrom and 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必须等于或大于检票口中选定的开始日期?
Now How can I put a validator stating that endDate must be equal to or grater than selected start date in wicket?
有什么主意吗?帮助表示赞赏.
Any idea? Help appreciated.
推荐答案
DateValidator.minimum(startdateField.getModel().getObject())
不起作用,因为在页面构建时,startdateField
的模型不保存用户提交的值,并且该值必须在验证时至少要考虑在内.
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.
通常,如果您的验证涉及多个组件,则应使用 validate()
方法/apache/wicket/markup/html/form/FormComponent.html#validate%28%29"rel =" nofollow> FormComponent.validate()
,因此确保在每个从属组件上都有有效的单独输入继续对它们进行完全验证.
Usually, if your validation involves more than a single Component, it's appropriate to use an IFormValidator
. Its validate()
method will be invoked after successful invokation of each dependent individual FormComponent.validate()
, so you're guaranteed to have valid individual inputs on each dependent component before proceeding on to validate them altogether.
验证的一个重要方面是防止无效的用户输入到达组件的模型.因此,在验证时,将不会更新模型,而是代替 FormComponent.getInput()
或
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 use FormComponent.getInput()
or FormComponent.getConvertedInput()
in the validate()
method.
IFormValidator validator = new AbstractFormValidator() {
public FormComponent<?>[] getDependentFormComponents() {
return new FormComponent[] { startDateField, endDateField };
}
public void validate(Form<?> form) {
Date startDate = (Date) startDateField.getConvertedInput();
Date endDate = (Date) endDateField.getConvertedInput();
if (endDate.before(startDate)){
error("Date range is invalid.");
}
}
};
form.add(validator);
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), the FormValidator
will not execute.
您可能还会发现此信息有用:验证相关字段
You may also find this information useful: Validating related fields
这篇关于检票口日期范围(从-到)验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!