2013-07-09 34 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> 
相关问题