比较使用相同类的两个字段 [英] Compare two fields that use same class

查看:70
本文介绍了比较使用相同类的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个输入字段fromDate和toDate,它们是Date类的实例. Date类使用自定义的Date验证器来验证月,日和年字段 包含在日期字段中. 自定义日期验证器特定于每个日期,即fromDate和toDate. 我需要将fromDate的月,日或年字段与toDate进行比较. 如果fromDate大于toDate,则必须显示一条验证消息.

I have two input fields fromDate and toDate which are instances of Date class. The Date class uses custom Date validator which validates the month, day and year fields contained in the date field. The custom date validator is specific for each date i.e, fromDate and toDate. I need to compare the month, day or year fields of fromDate with toDate. If the fromDate is greater than toDate, a validation message has to displayed.

更新:

fromDate和toDate是两个自定义日期组件,如下所示:

The fromDate and toDate are two custom date components as below

<eg:dateField id="inpFromDate" value="#{mrBean.fromDate}" .... />
<eg:dateField id="inpToDate" value="#{mrBean.toDate}" .... />

fromDate和toDate是Date类的实例,

fromDate and toDate are instances of Date class which is

public class Date {
private String mm;
private String dd;
@customDateValidator   //Validates each date field
private String yyyy;
//constructors
//getters and setters

在每个日期已经有一个验证器的情况下,您将如何实现验证器

How would you implement the validator in this case where each date already has a validator

推荐答案

是的,可以!假设您具有以下PrimeFaces的输入字段:

Yes, you can! Suppose you have the following PrimeFaces's input fields:

<p:calendar id="from" value="#{mrBean.fromDate}" binding="#{from}" >
   <p:ajax process="from to" update="toDateMsg" />
</p:calendar>
<p:calendar id="to"   value="#{mrBean.toDate}" >
   <f:attribute name="fromDate" value="#{from.value}" />
   <f:validator validatorId="validator.dateRangeValidator" />
   <p:ajax process="from to" update="toDateMsg" />
</p:calendar>
<p:message for="to" id="toDateMsg" />

这应该是您的Validator:

@FacesValidator("validator.dateRangeValidator")
public class DateRangeValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null || component.getAttributes().get("fromDate") == null) return;

        Date toDate   = (Date) value; 
        Date fromDate = (Date) component.getAttributes().get("fromDate");

        if (toDate.after(fromDate)) {
            FacesMessage message = new FacesMessage("Invalid dates submitted.");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }
}

请注意,我正在使用PrimeFaces的<p:calendar>组件编写示例,因为绑定到该组件的属性将在验证之前自动转换为Date对象.在您的程序中,您可能拥有自己的Converter来将String转换为Date.

Note that I am using PrimeFaces's <p:calendar> component to write my example because the properties binded to this component will automatically be converted to Date object before being validated. In your program, you may have your own Converter to convert String to Date.

这篇关于比较使用相同类的两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆