1

I a have a JSF page with PrimeFaces with some input fields. Before a submit is done, I would like to use the value of a field as input for a method which does some calculations and updates another field with the result but without submitting the form.

This is the field that will be used as input for my method:

<h:outputLabel for="hiredate" value="#{msgs['addUser.hireDate']}" />
                                <p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" immediate="true"/>
                                <p:message for="hiredate" />

The calculation is done by clicking a <p:commandButton>:

<p:commandButton value="Calculate days" icon="ui-icon-circle-check" action="#{userWizardMB.calculateVacationDays}" update="vacationDays" process="@this" immediate="true"/>

And this is the method called:

public void calculateVacationDays() {
        user.setVacationDays((int) vacationDaysCalculator
                .calculateWithHireDate(user.getHireDate()));
}

When debugging, though, I see that this field is NULL even if I set value in the form.

How can I force the setting of this field - user.hireDate because I really need this value for my calculation?

Thank you

Edit: I removed all of the other fields in the form and the immediate attribute:

<h:form id="addUserForm">
    <p:wizard widgetVar="wizard" flowListener="#{userWizardMB.onFlowProcess}">
                <!-- TAB FOR PERSONAL DATA -->
                    <p:tab id="personal" title="#{msgs['addUser.personalTab']}">
                        <p:panel header="#{msgs['addUser.personalInformation']}">
                            <p:message for="vacationDays" showDetail="true" autoUpdate="true" closable="true"/>
                            <h:panelGrid columns="3" columnClasses="label, value" styleClass="grid">

                                <h:outputLabel for="hiredate" value="#{msgs['addUser.hireDate']}" />
                                <p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" />
                                <p:message for="hiredate" />


                                <h:outputLabel for="vacationDays" value="#{msgs['addUser.vacationDays']}"/>
                                <p:inputText id="vacationDays" value="#{userWizardMB.user.vacationDays}"/>
                                <p:commandButton value="Calculate days" icon="ui-icon-circle-check" action="#{userWizardMB.calculateVacationDays}"  process="@this hiredate" update="vacationDays"/>
                </h:panelGrid>
            </p:panel>
        </p:tab>
    </p:wizard>
</h:form>

And the backing bean method is still not called.

AndaP
  • 1,308
  • 8
  • 23
  • 40

3 Answers3

1

Remove immediate="true" from the input and the command component. Do not use immediate unless you really understand what it should be used for. Further you also need to include the input component which you'd like to process in the update attribute of the command component. Note that this should represent the client ID, not the property name as mentioned in one of your comments.

<p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" />
...
<p:commandButton value="Calculate days" icon="ui-icon-circle-check" 
    action="#{userWizardMB.calculateVacationDays}" 
    process="@this hiredate" update="vacationDays" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I added everything you suggested and my backing bean method is not being called anymore (userWizardMB.calculateVacationDays)?! I don't know why this happened, I read somewhere it could happen because of the validation of the other fields but even when they all have valid values it is not called :( – AndaP Dec 14 '12 at 12:22
  • The given snippet works when placed in its own ``. Give it a try first to exclude the other components which you omitted from the question from being the actual cause of the problem. – BalusC Dec 14 '12 at 12:23
0

Use process="@form" (or process="[the id of the calendar component]" in the commandButton

process="@this" means that only the part of the model related to the commandButton (usually none) gets updated.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

Old question, but did you add partialSubmit="true" in the commandButton tag? At least in PrimeFaces 3.5, false is the default value of this attribute (see the PrimeFaces PDF documentation).

Santiago
  • 51
  • 6