2009-06-29 17 views
1

我需要在接縫中驗證兩個用戶輸入字段。對於ui:repeat標記中的每一行,Field1必須大於Field2。到目前爲止,我有兩個字段包裝在s:decorate標籤中,該標籤將所有輸入包裝在s:validateAll標籤中。這使我可以將錯誤消息浮動到字段右側,如果任何一個驗證失敗的話。如何使接縫中的字段失效?

例如(我不能插入圖像,所以我必須使用ASCII圖片,原諒低質請,斜體字表示紅色文本):

標籤: |黃色| 0 |%紅色:| 0%| | 黃色和紅色必須介於0和100之間,黃色必須大於紅色。

標籤: |黃色| 0 |%紅色:| 0%| | 黃色和紅色必須介於0和100之間,黃色必須大於紅色。

這兩個控件和裝飾xhtml如下。注意:「0到100之間的值」驗證已通過hibernate註釋處理。我只需要需要知道如何驗證這兩個字段以確保黃色大於紅色,並且仍然顯示錯誤消息。

我希望的解決方案是設置相應的s:decorate標籤的#{invalid}屬性,這樣錯誤信息就會顯示出來,但我會採取任何想法。

的輸入:

<table> 
    <ui:repeat value="#{action.List}" var="var"> 
     <s:decorate template="/layout/decorateMultipleInputs.xhtml" > 
      <ui:define name="label"> 
       Label: 
      </ui:define> 
      <ui:define name="input"> 
       <h:panelGrid columns="8" frame="border"> 
        <h:outputText value="Yellow:" /> 
        <h:inputText value="#{var.yellow}" style="width:25px; text-align:right" maxlength="3"/> 
        % 

        <h:outputText value="Red:" /> 
        <h:inputText value="#{var.red}" style="width:25px; text-align:right" maxlength="3"/> 
        % 
       </h:panelGrid> 
      </ui:define> 
      <ui:define name="message">Yellow and Red must be between 0 and 100, and Yellow must be greater than Red. 
      </ui:define> 
     </s:decorate> 
    </ui:repeat> 
</table> 

和decorateMultipleInputs.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:s="http://jboss.com/products/seam/taglib"> 
<tr> 
<td>   
    <s:label styleClass="#{invalid?'error':''}"> 
     <ui:insert name="label"/> 
     <s:span styleClass="required" rendered="#{required}">*</s:span> 
    </s:label> 
</td> 
<td> 
    <s:validateAll> 
     <ui:insert name="input"/> 
    </s:validateAll> 
</td> 
<td> 
    <s:div styleClass="error" rendered="#{invalid}"> 
     <h:graphicImage value="/images/error.gif" /> 
    </s:div>  
</td> 
<td> 
    <s:div styleClass="error" rendered="#{invalid}"> 
     <ui:insert name="message"/> 
    </s:div>  
</td> 
</tr> 
</ui:composition> 

回答

1

我將自己的定製JSF驗證連接到紅色<h:inputText/>

<h:inputText value="#{var.red}" style="width:25px; text-align:right" maxlength="3"> 
    <f:validator validatorId="rowValidator"/> 
</inputText> 

實現一個JSF驗證器(因爲你使用Seam,你可以使用@Validator註釋)。

@Validator 
public class RowValidator implements javax.faces.validator.Validator 
{ 
    public void validate(FacesContext context, UIComponent component, Object value) 
     throws ValidatorException 
    { 
     <snip/> 
    } 
} 

這裏的關鍵是傳遞給validate()方法的UIComponent對象。這是你已經綁定驗證器的<h:inputText/>。在這裏,您可以撥打getParent()得到<h:inputText/>父(<h:panelGrid/>)。現在,您可以枚舉<h:panelGrid/>子對象找到黃<h:inputText/>,檢索傳遞的價值,並與傳遞給validate()方法value參數進行比較。

如果黃小於紅色,你可以做你的validate()方法如下:

FacesMessage message = new FacesMessage(); 
message.setDetail("Yellow must be greater than red"); 
message.setSummary("Yellow must be greater than red"); 
message.setSeverity(FacesMessage.SEVERITY_ERROR); 
throw new ValidatorException(message); 
+0

偉大的想法,我會嘗試,謝謝! – 2009-06-30 19:17:47

0

煤層目前還沒有辦法做到多領域的驗證。目前,這是JSR-299 WebBeans的文件,但如何做到這一點,並沒有一個清晰而明確的方式。

您可以通過驗證後一種形式提交爲你平時的操作處理程序的一部分實現這一目標。即。

public String processRedsAndYellows() { 
    for(RedYellow var : ActionBean.getList()) { 
     if(var.getYellow() <= var.getRed()) { 
     messages.addMessage("All Yellows must be greater than Reds"); 
     return null; 
     } 
    } 
    return "success"; 
} 

或者這個效果。 :)

+0

感謝您的快速反應,我怕我不得不走這條路。 :) – 2009-06-29 22:15:16