2013-07-09 35 views
0

我在我的jsf中獲得了一個數字驗證器和一個複選框。當一個複選框被選中時,數字驗證器將檢查驗證。當複選框未被選中時,驗證將跳過它。當點擊複選框時動態禁用驗證器

請看看我的代碼

<h:outputText value="#{trancheResources.label_enable}:" /> 
<p:selectBooleanCheckbox id="enableCheckBox" itemLabel="#{trancheResources.label_yes}" value="#{trancheBean.enableCheck}" disabled="#{trancheBean.readonly}"> 

</p:selectBooleanCheckbox> 

<p:outputLabel for="acceptableMinVal" value="#{trancheResources.label_acceptableMinVal}:" /> 
<pe:inputNumber id="acceptableMinVal" value="#{trancheBean.trancheValidation.podMin}" disabled="#{trancheBean.readonly}" maxValue="999" 
       required="#{trancheBean.requiredIfEnableCheck}" requiredMessage="#{trancheResources.label_acceptableMinVal} is required."> 
    <f:validateDoubleRange disabled="#{trancheBean.cValidation}" minimum="1.00" /> 
</pe:inputNumber> 

<p:outputLabel for="acceptableMaxVal" value="#{trancheResources.label_acceptableMaxVal}:" /> 
<pe:inputNumber id="acceptableMaxVal" value="#{trancheBean.trancheValidation.podMax}" disabled="#{trancheBean.readonly}" maxValue="999" 
       required="#{trancheBean.requiredIfEnableCheck}" requiredMessage="#{trancheResources.label_acceptableMaxVal} is required."> 
    <p:ajax event="keyup" listener="#{trancheBean.acceptableMaxValOnkeyup}" ></p:ajax> 
</pe:inputNumber> 

<p:outputLabel for="exceptionMinVal" value="#{trancheResources.label_exceptionMinVal}:" /> 
<pe:inputNumber id="exceptionMinVal" value="#{trancheBean.trancheValidation.podExceptionMin}" disabled="#{trancheBean.readonly}" maxValue="999"/> 

<p:outputLabel for="exceptionMaxVal" value="#{trancheResources.label_exceptionMaxVal}:" /> 
<pe:inputNumber id="exceptionMaxVal" value="#{trancheBean.trancheValidation.podExceptionMax}" disabled="#{trancheBean.readonly}" maxValue="999"/> 

請指引我到一個解決方案。我不知道如何解決這個問題。

+0

哎,你可以有兩個輸入控制一個與驗證和一個未經驗證。並根據檢查複選框的事件,您可以逐個顯示它們。希望這可以幫助。 –

+0

或者他可以像'immediate =#{myBean.someCondition}'那樣在'immediate'屬性中表達一個表達式。當複選框被選中時,'someCondition'評估爲'false',當它被取消選擇時,它變爲'true'(所有這些都通過ajax更新)。 – Andy

+0

嗨,謝謝。我先嚐試。順便說一句可以給我一些樣本答案? – user998405

回答

0

我會爲您提供此示例答案,您可以使用它將其應用於您的。這種方法是基於Pankaj Kathiriya的評論,因爲這似乎是你想要做的。

在下面的示例代碼中,您有兩個<h:inputText>(將此組件替換爲您的<pe:inputNumber>)。每當您選中/取消選中<p:selectBooleanCheckBox>時,它們的rendered屬性值都會更改。當rendered評估爲true時,將出現帶驗證的<h:inputText>,未驗證的消失(反之亦然)。

<selectBooleanCheckBox>將在每次檢查/取消選中時觸發ValueChangeEvent。您還需要確保將immediate設置爲true,以便可以先處理它(前一個階段)。然後在您的監聽器中撥打renderResponse跳過剩餘的生命週期。如果您沒有驗證,驗證將通過驗證進行驗證,您將在切換髮生時看到驗證錯誤消息。最後,對於<h:selectBooleanCheckBox>,您希望在發生選擇/取消選擇時提交表單。這可以通過將其onchange屬性設置爲submit()(例如onchange = "submit()")來通過javascript完成。嘗試運行代碼,這樣做可能都有道理。

再次,請記住,這只是一個示例,可以幫助您指導您的解決方案。

public class SampleBean { 
    private boolean renderValidatedForm; 
    private String firstInput; 
    private String secondInput; 

    //Constructor ommitted 
    //Setters and getters ommitted 

    public void toggleRender(ValueChangeEvent e) { 
     boolean valueCheck = (Boolean) e.getNewValue(); 
     renderValidatedForm = valueCheck; 
     FacesContext.getCurrentInstance().renderResponse(); 
    } 
} 

XHTML的

<h:form> 
    <h:panelGrid> 
     <h:panelGroup> 
      <h:outputLabel for="enableCheckBox" value="Check to view form with validation"/> 
      <p:selectBooleanCheckbox id="enableCheckBox" value="#{sampleBean.renderValidatedForm}" 
            onchange="submit()" 
            valueChangeListener="#{sampleBean.toggleRender}" 
            immediate="true"/> 
     </h:panelGroup> 
     <h:panelGroup> 
      <h:outputLabel for="withValidation" value="Form with validation" 
          rendered="#{sampleBean.renderValidatedForm}"/> 
      <h:inputText id="withValidation" value="#{sampleBean.firstInput}" 
         required="true" rendered="#{sampleBean.renderValidatedForm}"/> 
      <h:message for="withValidation"/> 
     </h:panelGroup> 
     <h:panelGroup> 
      <h:outputLabel for="noValidation" value="Form without validation" 
          rendered="#{!sampleBean.renderValidatedForm}"/> 
      <h:inputText id="noValidation" value="#{sampleBean.secondInput}" 
         rendered="#{!sampleBean.renderValidatedForm}"/> 
     </h:panelGroup> 
    </h:panelGrid> 
    <h:commandButton value="Submit"/> 
</h:form> 
相關問題