2011-12-08 124 views
1

當其他字段驗證失敗時,我需要從驗證程序中清除輸入文本。我試過使用setSubmittedValue("")setValue(""),但它不起作用。在其他字段驗證失敗時清除表單字段

我想這樣做的原因是因爲該字段是一個驗證碼字段,如果其他字段驗證失敗,頁面將呈現一個新的驗證碼,我希望輸入字段爲空。

形式:

<h:form id="form"> 
    <h:inputText id="otherfieldid" required="true" 
     requiredMessage="Please enter" 
     validator="#{bean.validateA}" 
     validatorMessage="Validation fails" /> 

    <h:inputText id="fieldid" required="true" 
     requiredMessage="Please enter" 
     validator="#{bean.validateB}" 
     validatorMessage="Validation fails" /> 
</h:form> 

豆:

@ManagedBean 
@ViewScoped 
public Bean { 
    public void validateA(FacesContext context, 
       UIComponent componentToValidate, 
       Object value) 
       throws ValidatorException { 
     /* validation codes */ 
    } 

    public void validateB(FacesContext context, 
       UIComponent componentToValidate, 
       Object value) 
       throws ValidatorException { 

     HtmlInputText ht = 
      (HtmlInputText) context.getViewRoot().findComponent(":form:otherfieldid"); 

     if (ht != null) {   
      if (ht.getValidatorMessage() != null 
        || !"".equals(ht.getValidatorMessage())) {    
       ((HtmlInputText) componentToValidate).setSubmittedValue(""); 
       return; 
      } 
     } 
     /* validation codes */ 
    } 
} 

回答

0

哪裏是你value="#{yourBeam.field}的輸入字段綁定?應該可以將Bean中的這個字段設置爲null,如果驗證失敗...

+0

我忘了補充一點,我嘗試了並且綁定了,兩者都不起作用。 –