检票口日期范围(从-到)验证

发布于 2024-12-07 21:21:23 字数 1154 浏览 0 评论 0 原文

我有一个表单需要验证 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 必须等于或大于检票口中选定的开始日期?

有什么想法吗?帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2024-12-14 21:21:23

DateValidator.minimum(startdateField.getModel().getObject()) 不起作用,因为在页面构建时,startdateField 的模型不保存用户提交,并且在验证时必须将其作为最低限度考虑。

通常,如果您的验证涉及多个组件,则适合使用 IFormValidator。它的validate() 方法将在成功调用每个依赖个体FormComponent。 validate(),因此在继续验证它们之前,您可以保证每个依赖组件都有有效的单独输入。

验证的一个重要方面是防止无效的用户输入到达组件的模型。因此,在验证时,模型尚未更新,而是 FormComponent.getModelObject(),您必须使用 FormComponent。 getInput()FormComponent。 validate() 方法中的 getConvertedInput()

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);

请考虑到,如果 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. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文