2011-05-29 71 views
2

我有以下FacesValidator:JSF自定義驗證:H:消息不被渲染

​​

至極被以下列方式使用:

<h:form id="profileform" action="#{userController.updatePassword}"> 
    <h:messages errorClass="error_message" globalOnly="true"/> 
    ... 
    <h:outputLabel for="password" value="Old password:" /> 
    <h:inputSecret id="password" name="password" label="Old password"> 
     <f:validateLength minimum="8" maximum="15" /> 
     <f:validator validatorId="passwordValidator"/> 
    </h:inputSecret> 
    <h:message for="password" errorClass="error_message"/> 
    ... 
</h:form> 

現在的問題是,該消息驗證器生成的內容從不顯示。我知道它就會產生,因爲在與GlassFish日誌,我可以看到

profileform:password: Old password is wrong! 

我看不出在這個任何錯誤特別是因爲被顯示爲f:validateLength的消息,如果密碼是長還是短。 如果我的

context.addMessage(comp.getClientId(context), msg); 

context.addMessage(null, msg); 

代替消息顯示在h:messages組件。 有沒有人有想法?在此先感謝

+0

想知道,你使用Seam 3嗎?由於通常PhaseListeners不是注入目標,注入實體管理器工廠不起作用。 – 2011-05-29 18:15:38

回答

5

你什麼都看不到,因爲你已經構建了FacesMessage總結的一個細節。當細節不是null時,它將被顯示。既然你已經設置了一個空字符串,你「看到」一個空的信息。您基本上需要將詳細信息設置爲null才能顯示摘要。

但這並不完全是您應該在驗證錯誤上設置消息的方式。你應該只是ValidatorException和JSF將根據組件的客戶端ID將ValidatorException構造的消息添加到上下文本身。

所以,你需要通過

String msg = "The old password was not entered correctly."; 
throw new ValidatorException(new FacesMessage(msg)); 

更換

FacesMessage msg = new FacesMessage(
     FacesMessage.SEVERITY_ERROR, 
     "The old password was not entered correctly.", 
     "" 
); 
context.addMessage(comp.getClientId(context), msg); 
throw new ValidatorException(msg); 

和你希望它會工作。

+0

你幫了我很多。非常感謝你! – setfire 2011-05-29 16:03:27

+0

不客氣。 – BalusC 2011-05-29 16:26:48