Question

Consider this simple example. If I have a form like this I obviously get a validation error when submitting with no value in the inputText:

<h:form id="testForm" prependId="false">

    <h:selectOneMenu 
        id="testSelectOneMenu" 
        value="#{backButtonTestBean.selection}">
        <f:selectItem itemValue="mickey" itemLabel="Mickey" />    
        <f:selectItem itemValue="mouse" itemLabel="Mouse" />
        <f:ajax execute="@this" render="panel" />
    </h:selectOneMenu>                 

       <h:panelGroup layout="block" id="panel">
           <h:panelGroup layout="block">
               <h:inputText id="requiredField" required="true" />
           </h:panelGroup>    
       </h:panelGroup>

       <h:commandButton value="submit" action="#{backButtonTestBean.submit}" />

</h:form>

But when I re-render the panelGroup w/ f:ajax the required attribute is not being honored and I can submit with an empty value:

<h:form id="testForm" prependId="false">

    <h:selectOneMenu 
        id="testSelectOneMenu" 
        value="#{backButtonTestBean.selection}">
        <f:selectItem itemValue="mickey" itemLabel="Mickey" />    
        <f:selectItem itemValue="mouse" itemLabel="Mouse" />
        <f:ajax execute="@this" render="panel" />
    </h:selectOneMenu>                 

       <h:panelGroup layout="block" id="panel">
           <h:panelGroup layout="block" rendered="#{backButtonTestBean.selection == 'mouse'}">
               <h:inputText id="requiredField" required="true" />
           </h:panelGroup>    
       </h:panelGroup>

       <h:commandButton value="submit" action="#{backButtonTestBean.submit}" />

</h:form>

I'm testing in mojarra 2.1.1. Any advice is appreciated.

Was it helpful?

Solution

update: I figured it out. BackButtonTestBean was in request scope so the value of testSelectOneMenu was not 'mouse' when I submitted the form. Consequently it wasn't rendered and thus the validators were invalid. Changed to @ViewScoped and works as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top