0
votes

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.

1
With "w/" did you mean to say "with"? I had a hard time in understanding this question.BalusC
Yes. It's a bad habit from chat and not for this site. I'll stop using it and edit this post accordingly.Dave Maple
For the case you didn't know, if you found the solution yourself, then you are eligible to post it as your own answer.BalusC
i didn't realize that. Is it considered good form to do so?Dave Maple

1 Answers

0
votes

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.