0

I got one number validator and one checkbox in my jsf. When a checkbox is selected then the number validator will check validation. When checkbox is unselected, the validation will skip it.

Please see my code

<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"/>

Please guide me to a solution. I have no idea on how to solve this.

4

1 回答 1

0

我将为您提供此示例答案,您可以将其应用于您的答案。这种方法基于 Pankaj Kathiriya 的评论,因为这似乎是您想要做的。

在下面的示例代码中,您有两个<h:inputText>(将此组件替换为 yours <pe:inputNumber>)。每次rendered您选中/取消选中<p:selectBooleanCheckBox>. 当rendered评估为true时,<h:inputText>带有验证的会出现,没有验证的会消失(反之亦然)。

每次您选中/取消选中它时都会<selectBooleanCheckBox>触发。ValueChangeEvent您还需要确保设置immediatetrue以便可以首先处理它(之前的一个阶段)。然后调用renderResponse你的听众跳过剩余的生命周期。<h:inputText>如果您不这样做,验证将启动with 验证,并且在切换发生时您将看到验证错误消息。最后,对于<h:selectBooleanCheckBox>,您希望在选择/取消选择发生时提交表单。这可以通过将 javascript 的onchange属性设置为submit()(eg onchange = "submit()") 来完成。尝试运行代码,这样这一切都有意义。

同样,请记住,这只是一个示例,可帮助您找到解决方案。

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>
于 2013-07-09T20:00:57.310 回答