2012-03-13 38 views
0

我的應用程序使用rich:calendars和h:inputText。如果我在intputText(即非數字數據)的UI中輸入不良數據,並按下「Search」,我會從bean中獲取正確的錯誤消息。但是,如果我在inputText(非數字)和rich:日曆(非日期相關的輸入,例如「foo」)中輸入了無效數據,我只會返回rich:calendar錯誤消息。就好像本地豐富的日曆驗證消息敲出了bean驗證消息。rich:日曆本機驗證隱藏/覆蓋/ ??我的bean驗證錯誤

如何獲取所有要顯示的消息?

的XHTML代碼如下:

<html xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:a4j="http://richfaces.org/a4j" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:tr="http://myfaces.apache.org/trinidad" 
     xmlns:rich="http://richfaces.org/rich"> 
<f:view> 
    <br/> 
    <div class="container"> 
    <tr:form id="searchCriteria" defaultCommand="appealSearchManager.search"> 
     <a4j:outputPanel id="errorMessagesPanel"> 
      <h:messages id="errorMessages"/> 
     </a4j:outputPanel> 
     <div class="div30"> 
      <p><h:outputText value="#{messages.ProgramInvoiceId}"/></p> 
      <h:inputText id="programInvoiceId" value="#{appealSearchManager.programInvoiceId}"/> 
     </div> 
     <div class="div30"> 
     <p><h:outputText value="#{messages.ResponseReleaseDate}"/></p> 
     <rich:calendar id="responseReleaseDateBegin" 
      enableManualInput="true" datePattern="MM/dd/yyyy" 
      buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar" 
      converterMessage="Invalid Response Release begin date. Format must be blah, blah, blah." 
      value="#{appealSearchManager.responseReleaseDateBegin}"> 
     </rich:calendar> 
     <rich:calendar id="responseReleaseDateEnd" 
      enableManualInput="true" datePattern="MM/dd/yyyy" 
      buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar" 
      converterMessage="Invalid Response Release End date. Format must be blah, blah, blah." 
      value="#{appealSearchManager.responseReleaseDateEnd}"> 
     </rich:calendar> 
     </div> 

     <div class="searchaction"> 
      <div> 
       <ul> 

        <li> 
         <a4j:commandLink id="searchButton" 
             value="#{messages.Search}" 
             actionListener="#{appealSearchManager.search}" 
             reRender="errorMessagesPanel, richErrorMessages, errorMessages" 
             styleClass="searchbtn"/> 
        </li> 
       </ul> 
      </div> 
     </div> 
    </tr:form> 
    </div> 
</f:view> 
</html> 

和bean代碼:

public void search(ActionEvent e) { 
    setHasErrors(validateCriteria()); 
} 

private boolean validateCriteria() { 
    boolean isValid = true; 
    //always check for Program Invoice ID may contain a comma-separated list of ids 
    if (getProgramInvoiceId() != null && !getProgramInvoiceId().equals("") && !areValidProgamInvoiceIds(getProgramInvoiceId())) { 
     String errorMessage = "Invalid Program Invoice ID. Must be comma delimited list of numbers."; 
     FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage)); 
     isValid = false; 
    } 
    //check Dates Response Release Date 
    if (!isValidDateRange("Response Release Date", getResponseReleaseDateBegin(), getResponseReleaseDateEnd())) { 
     isValid = false; 
    } 
    return isValid; 
} 

private boolean areValidProgamInvoiceIds(String ids) { 
    boolean validIds = true; 
    String regexp = "^([0-9]+(-[0-9]+)*,*)+$"; 
    Pattern pattern = Pattern.compile(regexp); 
    if (!pattern.matcher(ids).matches()) { 
     validIds = false; 
    } 
    return validIds; 
} 

private boolean isValidDateRange(String dateRange, Date startDate, Date endDate) { 
    boolean isValidDateAndRange = true; 
    Calendar futureDate = Calendar.getInstance(); 
    futureDate.roll(Calendar.DAY_OF_MONTH, 2); 
    if ((startDate != null && (endDate == null || endDate.equals(""))) || 
     ((startDate == null || startDate.equals("")) && endDate != null)) { 
     //one date is null and the other has a value. We need both values to do a search. 
     String errorMessage = "Invalid " + dateRange + " range. Missing Date. A date range must contain both start and end dates."; 
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage)); 
    } else if (startDate != null && endDate != null) { 
     if (isValidDateAndRange) { 
      //future start dates are not allowed 
      if (startDate.after(futureDate.getTime())) { 
       String errorMessage = "Invalid date range. Start date must be less than today's date."; 
       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage)); 
       isValidDateAndRange = false; 
      } 
      //start date greater than the end date is not allowed 
      if (endDate.before(startDate)) { 
       String errorMessage = "Invalid date range. End date must be greater than or equal to start date."; 
       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage)); 
       isValidDateAndRange = false; 
      } 
     } 
    } 
    return isValidDateAndRange; 
} 

任何建議,將不勝感激。 謝謝!

回答

0

您不應該在動作方法中執行驗證。您應該使用正常的Validator執行此操作,該操作通過<f:validator>或任何標準<f:validateXxx>標籤綁定。驗證失敗時從不調用操作方法。

例如,要驗證程序發票ID,請使用<f:validateRegex>

<h:inputText id="programInvoiceId" value="#{appealSearchManager.programInvoiceId}" validatorMessage="Invalid Program Invoice ID. Must be comma delimited list of numbers."> 
    <f:validateRegex pattern="^([0-9]+(-[0-9]+)*,*)+$" /> 
</h:inputText> 

驗證日期範圍有點複雜。至此,您需要將Validator類作爲<f:validator>放在其中一個組件上,並將另一個組件的值作爲屬性傳遞。有關示例,另請參閱Compare two fields that use same class

順便說一句,注意術語,你是而不是根本就是使用"bean validation",但你只是在動作方法中手動驗證。

+0

我喜歡使用的但要求狀態,我要收集所有的錯誤,並顯示他們一個jQuery的彈出式:-( – 2012-03-14 14:50:26

+0

順便說一句 - 感謝澄清術語....是一個新手我仍然在理解正確的單詞,我會嘗試驗證日期範圍,如上所述。謝謝! – 2012-03-14 14:53:10

+0

酷日期驗證!像魅力一樣工作。謝謝你!! – 2012-03-14 14:55:39