Search code examples
jsf-2

How to skip the validation for component which is being hidden?


I have a scenario like the below..

<h:selectOneRadio id="someId" value="#{myBean.type}" required="true">                               
    <f:ajax  event="valueChange"  execute="@form" render="myPanel">
    <f:selectItem itemLabel="Existing Type" itemValue="Existing Type" />
    <f:selectItem itemLabel="New Type" itemValue="New Type" />
<h:selectOneRadio>

<h:panelGroup id="myPanel" rendered="#{myBean.checkforNewType()}">
    <h:inputText id="txtval" value="#{mybean.val}" required = "true" requiredMessage="Some message">
    <h:message for="txtval" styleClass="error"/>
<h:panelGroup>

Basically the panel containing the textbox should be hidden if the value of the property type is "Existing Type". But the issue I am facing is if user leave the box blank the panel is not hidden as it fails the validation.

Is there anyway to avoid the validation when the panel containing the textbox is being hidden?


Solution

  • Better check the request parameter value instead. The model value is namely not updated when the validation has failed in general and thus your rendered condition will fail when being bound to a request scoped bean.

    <h:selectOneRadio id="someId" value="#{myBean.type}" required="true">                               
        <f:ajax event="valueChange" execute="@form" render="myPanel">
        <f:selectItem itemLabel="Existing Type" itemValue="Existing Type" />
        <f:selectItem itemLabel="New Type" itemValue="New Type" />
    <h:selectOneRadio>
    
    <h:panelGroup id="myPanel" rendered="#{param['formId:someId'] == 'New Type'}">
        <h:inputText id="txtval" value="#{mybean.val}" required="true" requiredMessage="Some message">
        <h:message for="txtval" styleClass="error"/>
    <h:panelGroup>
    

    Here, I assume that the parent <h:form> has an id="formId".