2013-03-17 58 views
1

我是JSF的新手,並且需要通過一個JSF應用程序。設置字段在jsf驗證後爲空

我想驗證一個空白字符串的密碼字段。

我知道,而不是在Javascript中做它,我打電話驗證功能。

我的需要是,如果密碼字段的值是空的,那麼它將從驗證器函數驗證到現在它正確地運行,但是在驗證之後,當它到達時= UI bav = ck那時密碼字段應該是空的。

如果密碼字段爲空(僅包含空格),那麼我希望它將其設置爲空白或保持原樣。

我嘗試到現在爲止, JSF視圖頁面singin.xhtml

    <h:outputText styleClass="outputBox" style="margin: 3px;" 
         value="Password" /> 
        <h:inputSecret id="password" required="true" 
         requiredMessage="Please enter your password" 
         value="#{userActionManager.dto.password}" 
         validator="#{userActionManager.validateSamePassword}"> 

          <a4j:ajax event="change" execute="@this" bypassUpdates="true" /> 
        </h:inputSecret> 

驗證方法。

public void validateSamePassword(FacesContext fc, UIComponent component, Object obj) { 
     System.out.println("UserActionManager.validateSamePassword()"); 

     String confirmPassword = (String)obj; 
     System.out.println("Password is :" + confirmPassword); 
      if(confirmPassword!=null && confirmPassword.trim().equals("")) { 
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password cannot be blank", " detail Passwords do not match!"); 

//   System.out.println(component.getFamily()); 
//   String field1Id = (String) component.getAttributes().get("password"); 

       // Find the actual JSF component for the client ID. 
//   UIInput textInput = (UIInput) fc.getViewRoot().findComponent("password"); 
//   textInput.setValue(""); 

       dto.setPassword(null); 

       throw new ValidatorException(message); 
      }else{ 
       dto.setPassword(confirmPassword); 
      } 
    } 

我試過選項dto.setPassword(null);但是當它返回查看時,密碼字段中的空格仍然存在,我必須手動刪除。

我在想什麼?

回答

1

在您的驗證,調用resetValue()作爲參數傳遞的UIComponent實例

public void validateSamePassword(FacesContext fc, UIComponent component, Object obj) { 
    System.out.println("UserActionManager.validateSamePassword()"); 

    String confirmPassword = (String)obj; 
    System.out.println("Password is :" + confirmPassword); 
     if(confirmPassword!=null && confirmPassword.trim().equals("")) { 
      FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password cannot be blank", " detail Passwords do not match!"); 

      component.resetValue(); 

      throw new ValidatorException(message); 
     }else{ 
      dto.setPassword(confirmPassword); 
     } 
} 

dto.setPassword(null)不會影響在密碼框中的值作爲輸入部件的設計都保留導致他們無法驗證值。 resetValue()是一種方法,將導致組件將其值重置爲未初始化狀態

編輯:上述解決方案提前重置輸入值。從技術上講,驗證不會失敗,直到拋出一個ValidationException。爲了有效地復位場:

  1. 定義一個偵聽器事件將重置組件的價值只有在驗證失敗

    public void resetPwd(ComponentSystemEvent evt) throws IOException { 
    HtmlInputSecret txt = (HtmlInputSecret) evt.getComponent(); 
    if(!txt.isValid()){ //make sure that the validation failed 
        txt.resetValue(); 
        } 
    
    } 
    
  2. 裝上監聽事件的postValidate事件偵聽器的密碼組件。這將確保僅在驗證失敗後才重置該值。

    <h:inputSecret id="password" required="true" 
           requiredMessage="Please enter your password" 
           value="#{userActionManager.dto.password}" 
           validator="#{userActionManager.validateSamePassword}"> 
            <f:event name="postValidate" listener="#{userActionManager.resetPwd}"/> 
            <a4j:ajax event="change" execute="@this" bypassUpdates="true" /> 
          </h:inputSecret> 
    

這裏的關鍵是將值在正確的時間重置。其他時間選項包括preRenderView事件和postAddToView事件。這是在所有的時間

+0

component.resetValue

dto.setPassword(null); 

(); UIComponent上沒有這種方法。我嘗試像((UIInput)組件).resetValue(),我發現在UIInput中有resetValue。仍然沒有成功。空白值仍然存在於Field中,我想說清楚。 – Jayesh 2013-03-17 07:54:08

+0

@JayeshPatel,其實這是有道理的。我的錯。從技術上講,只有拋出'ValidationException'時,驗證纔會失敗。我提出的當前解決方案是過早地重置值。查看我的更新以獲取更適合的時機。 – kolossus 2013-03-17 08:34:17

+0

'resetValue()'未在'UIComponent'中定義。它在'EditableValueHolder'接口中定義,它由其他'UIInput'實現。 – BalusC 2013-03-17 15:07:41

1

如果一個組件被標記爲無效,那麼它將不會重新顯示模型值(在你的情況下,從dto的值)。相反,它將重新顯示其submitted value。您只需將提交的值設置爲空字符串即可。

替換由

((UIInput) component).setSubmittedValue("");