0

This is an update on a previous post I made regarding conditional rendering of page components. I can now conditionally render different components on the page based on selected user inputs using the f:ajax tag. The trouble is that when I click my commandbutton the inputs that are conditionally rendered using ajax aren't being read on submit. I am guessing it is because the whole page isn't rerendered when I add in the new components and the submit button can't see them, even though they are there. Here is the segment of my form page(it is wrapped in a h:form tag):

            <h:outputLabel for="selectPersonType" value="Select Type: "/>
            <h:selectOneMenu id="selectPersonType" value="#{addPersonBean.personType}" label="Person Type" required="true">
                <f:selectItem itemValue=""/>
                <f:selectItems value="#{listBean.personTypes}" />
                <f:ajax render="results"/>
            </h:selectOneMenu>
            <h:message for="selectPersonType" style="color: red"/>
        </h:panelGrid>

        <h:panelGroup id="results">
            
            <h:panelGroup rendered="#{addPersonBean.personType == 'STUDENT'}">
                <h:outputLabel for="selectSchoolType" value="School Type: " />
                <h:selectOneMenu id="selectSchoolType" value="#{addPersonBean.schoolType}">
                    <f:selectItems value="#{listBean.schoolTypes}" />
                    <f:ajax execute="selectSchoolType"/>
                </h:selectOneMenu>
            </h:panelGroup>
            
            <h:panelGroup rendered="#{addPersonBean.personType == 'PATIENT'}">
                <h:outputLabel for="smoker" value="Smoker? " />
                <h:selectBooleanCheckbox id="smoker" value="#{addPersonBean.smoker}">
                    <f:ajax execute="smoker"/>
                </h:selectBooleanCheckbox>
  
            </h:panelGroup>
        </h:panelGroup>

        <h:commandButton value="Add Person" action="#{addPersonBean.action}"/>

The trouble here is that the components with ids 'selectSchoolType' and 'smoker's values don't get set when I click the commandButton because they are rendered conditionally using the selectPersonType select menu after the page has loaded. Is there a way to fire the components when their values change instead of when I click the submit button (therefore, their values should be processed before I even click submit). As you can see I have tried to use f:ajax with the execute attribute attached to the components in question but it didn't seem to work. (I believe the default events for these components are valueChange so haven't added them to the f:ajax tags, but I have tried 'change' and 'valueChange').

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Alan Smith
  • 1,069
  • 4
  • 19
  • 23
  • can your write down a simplified scenario that not working as you expected it to ? e.g : I click on this... but that not being done... – Daniel Nov 28 '12 at 11:40
  • But it is simplified. I click the commandButton and the values of components with ids 'selectSchoolType' and 'smoker' don't get sent to the backing beans setters. They get sent as null, even though they are selected/checked. – Alan Smith Nov 28 '12 at 12:28
  • how about adding `` , like this`` – Daniel Nov 28 '12 at 12:31

1 Answers1

1

All your <f:ajax> things are inside a conditionally rendered component. This construct will fail when the condition behind rendered attribute evaluates to false during processing the form submit. This will happen when the #{addPersonBean} is in the request scope and doesn't preinitialize the condition behind the rendered attribute during (post)construction, or when it's performing business logic in getters/setters in a wrong way.

Placing the #{addPersonBean} in the view scope and ensuring that you aren't doing business logic in getters/setters should fix this problem.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Excellent!! I simply changed the bean scope from request to view like you said and it worked! The information you give in the two links you provided are very helpful, thank you. Did you happen to write JSF or something ;) – Alan Smith Nov 29 '12 at 15:26