I have a form where I need to validate DateFrom and DateTo.
I have done like this:
// 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 ?
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())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), theFormValidatorwill not execute.You may also find this information useful: Validating related fields