开发者

wicket date range (From-To) validation

开发者 https://www.devze.com 2023-04-10 20:21 出处:网络
I have a form where I need to validate DateFrom and DateTo. I have done like this: // start date RequiredTextField<Date> startdateField =

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(sta开发者_开发百科rtdateField.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. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号