pgpb.padilla
pgpb.padilla

Reputation: 2418

Bean property not updated on action

I have the following Facelet page:

        <ui:define name="content">

            <h:form id="selectReport">

                <div>
                    <h:selectOneMenu value="#{reportsController.selectedReport}">
                        <f:selectItems value="#{reportsController.reportsList}"/>
                    </h:selectOneMenu>
                </div>
                <div>

                    <h:commandButton value="Ir al reporte"
                                     action="#{reportsController.goToReport}"/>

                </div>
            </h:form>

            <h:form id="dateRangeSetupForm"
                    rendered="#{reportsController.showDateRangeSetupForm}">
                <div>
                    <h:outputLabel value="Desde:"/>
                    <h:inputText id="fromDate"
                                 value="#{reportsController.fromDate}"/>
                </div>
                <div>
                    <h:outputLabel value="Hasta:"/>
                    <h:inputText id="toDate"
                                 value="#{reportsController.toDate}"/>
                </div>
            </h:form>

            <h:form id="billOfLadingSetupForm"
                    rendered="#{reportsController.showBillOfLadingSetupForm}">
                <div>
                    <h:outputLabel value="# Factura:"/>
                    <h:inputText id="invoiceNumber"
                                 value="#{reportsController.invoiceNumber}"/>
                </div>
            </h:form>


            <h:form id="submitForm">
                <h:commandButton id="processReport" value="Generar Reporte"
                                 action="#{reportsController.processReport}"/>
                <h:commandButton id="goBack" value="Regresar"
                                 action="mainMenu"/>
            </h:form>

        </ui:define>

In which I let the user select a report, and render a form to setup parameters depending on the report. The problem is that when I render for example the billOfLadingSetupForm and fill the inputText, then click on the processReport button the invoiceNumber property of my bean is not updated, it is set to null.

I have noticed that if I type Return after filling the inputText and then click the processReport button the property does get updated. This is strange I haven't face this problem before. Do you have any idea why this happens?

Upvotes: 0

Views: 1789

Answers (1)

BalusC
BalusC

Reputation: 1109635

Whenever a form get submitted, then only the data inside that form will be sent. The data of the other forms won't be sent. That's by HTML specification. You need to put all inputs and buttons which belongs to each other inside the same <h:form>. Replace all your splitted dateRangeSetupForm, billOfLadingSetupForm and submitForm forms by <h:panelGroup> and put them all inside one and same <h:form>.

Something like this:

<ui:define name="content">
    <h:form>
        <div>
            <h:selectOneMenu value="#{reportsController.selectedReport}">
                <f:selectItems value="#{reportsController.reportsList}"/>
            </h:selectOneMenu>
        </div>
        <div>
            <h:commandButton value="Ir al reporte" action="#{reportsController.goToReport}"/>
        </div>
    </h:form>

    <h:form>
        <h:panelGroup id="dateRangeSetupForm" rendered="#{reportsController.showDateRangeSetupForm}">
            <div>
                <h:outputLabel value="Desde:"/>
                <h:inputText id="fromDate" value="#{reportsController.fromDate}"/>
            </div>
            <div>
                <h:outputLabel value="Hasta:"/>
                <h:inputText id="toDate" value="#{reportsController.toDate}"/>
            </div>
        </h:panelGroup>

        <h:panelGroup id="billOfLadingSetupForm" rendered="#{reportsController.showBillOfLadingSetupForm}">
            <div>
                <h:outputLabel value="# Factura:"/>
                <h:inputText id="invoiceNumber" value="#{reportsController.invoiceNumber}"/>
            </div>
        </h:panelGroup>

        <h:commandButton id="processReport" value="Generar Reporte" action="#{reportsController.processReport}"/>
        <h:commandButton id="goBack" value="Regresar" action="mainMenu"/>
    </h:form>
</ui:define>

Make sure that the bean is put in the view scope. Otherwise the conditions for the rendered attribute won't be preserved. See also commandButton/commandLink/ajax action/listener method not invoked or input value not updated

Upvotes: 1

Related Questions