两个Primefaces日历组件验证 [英] Two primefaces calendar component validation

查看:86
本文介绍了两个Primefaces日历组件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JSF 2 中有一个表单,在其中使用双精度字段来指定日期范围.这样做的目的不是让用户选择在第二个日期之前的第一个日期.因此,我想使用p:calendar组件在发送表单之前执行验证.我要做的是将验证器绑定到第二个日历输入,以便在内部访问第一个组件并比较日期.

I've a form in JSF 2 where I'm using a double field to specify a range of dates. The aim of that is not to let the user to pick a first date which is before the second one. So I want to perform a validation before the form is sent, using p:calendar components. What I do is to tie a validator to the second calendar input in order to access the first component internally and compare dates.

这是我的 xhtml 页面:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head />
<h:body>
    <h:form id="date_form">
        <p:calendar id="date1input" value="#{dateTestBean.date1}" />
        <p:calendar value="#{dateTestBean.date2}" validator="dateValidator">
            <f:attribute name="date1" value="#{date1input}" />
        </p:calendar>
        <p:commandButton value="Submit" action="#{dateTestBean.submit}"
            ajax="false" />
    </h:form>
</h:body>
</html>

我的托管bean:

@ManagedBean
@ViewScoped
public class DateTestBean {

    private Date date1;

    private Date date2;

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

    public void submit() {
        System.out.println("Submited " + date1 + " " + date2);
    }

}

我的验证者类:

@FacesValidator(value = "dateValidator")
public class DateValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        System.out.println(((UIInput) context.getViewRoot().findComponent(
                "date_form:date1input")).getValue());
        UIInput date = (UIInput) component.getAttributes().get("date1");
        System.out.println(date);
        //Will perform date comparison
    }
}

我在这里发送的输出为2013-10-10作为第一个日期,而2013-10-12作为第二个日期为:

The output I get here sending 2013-10-10 as first date and 2013-10-12 as second one is:

2013年10月10日星期四00:00:00

Thu Oct 10 00:00:00 CEST 2013

2013年10月10日星期四00:00:00(CEST 2013年)提交

Submited Thu Oct 10 00:00:00 CEST 2013 Sat Oct 12 00:00:00 CEST 2013

建议f:attribute标记不适用于p:calendar组件

Which suggests f:attribute tag is not working for p:calendar component as it does for other input components. However, I can access the first calendar via view root, but enforces me to know the entire path of the client id of the component I want to validate. Both values are set in the managed bean later with no issues.

一种解决方法是使用Ajax在第一个日期更改时调用,以便将其放置在模型中并作为日期本身发送到f:attribute中,而不是发送UIInput(我已经这样做了).

A workaround would be to use an ajax call when first date changes in order to place it in the model and send it as date itself in the f:attribute instead of sending the UIInput (I'm already doing it that way).

没有更好的方法吗?也许我必须将它们加入单个组件中,例如 ?

Isn't there a better way? Perhaps I have to join them in a single component like in this case?

推荐答案

从您的代码中

<p:calendar id="date1input" ... />
<p:calendar ...>
    <f:attribute name="date1" value="#{date1input}" />
</p:calendar>

#{date1input}不由id="date1input"表示.如这些代码示例所示,它由binding="#{date1input}"表示.

The #{date1input} isn't represented by id="date1input". As shown on those code examples, it's represented by binding="#{date1input}".

相应修复:

<p:calendar binding="#{date1input}" ... />
<p:calendar ...>
    <f:attribute name="date1" value="#{date1input}" />
</p:calendar>

另请参见:

  • 在JSF中'binding'属性如何工作?何时以及如何使用?
  • See also:

    • How does the 'binding' attribute work in JSF? When and how should it be used?
    • 这篇关于两个Primefaces日历组件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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