wicket date range (From-To) validation

余生颓废 提交于 2020-01-13 19:42:07

问题


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.


回答1:


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. 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.

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



来源:https://stackoverflow.com/questions/7650104/wicket-date-range-from-to-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!