Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are fields reset after ajax update with Primefaces

On a form I have some personal information (name, address etc.) and a changeListener which updates the salutation and letter salutation according to the gender and last name. If the underlying entity was already stored in the database it works fine. If I'm entering data for a new (not saved) entry the form gets reset after the listener has been called. All data entered is lost, only the salutation, lettersalutation which I modify in the listener and gender, name which I use and attached a ajax call on are kept.

This is a part of the form:

<h:form id="person">
  <p:panel header="#{msg['prs']}">
    <h:panelGrid columns="6" cellpadding="4">
      <h:outputText value="#{msg['prs.salutation']}"/>
      <p:inputText value="#{personBean.selectedPerson.salutation}"/>

      <h:outputText value="#{msg['prs.lettersalutation']}"/>
      <p:inputText value="#{personBean.selectedPerson.letterSalutation}"/>
      <p:spacer/><p:spacer/>

      <h:outputText value="#{msg['prs.name']}: "/>
      <p:inputText value="#{personBean.selectedPerson.name}">
        <p:ajax event="change" update="person"
          listener="#{personBean.selectedPerson.updateSalutation}" />
      </p:inputText>

      <h:outputText value="#{msg['prs.surname']}: "/>
      <p:inputText value="#{personBean.selectedPerson.surname}"/>

      <h:outputText value="#{msg['prs.gender']}: "/>
      <p:selectOneMenu value="#{personBean.selectedPerson.gender}">
        <f:selectItems value="#{enumHelper.getEnum('Gender')}"/>
         <p:ajax event="change" update="person" 
           listener="#{personBean.selectedPerson.updateSalutation}" />
      </p:selectOneMenu>
  </p:panel>
</h:form>

In the code I do then the updates.

public void updateSalutation() {
    // simplified
    letterSalutation = "...";
    salutation = "...";
    // outputs for debug
    System.out.println(this.getName()); // --> not null
    System.out.println(this.getSurname()); // --> null
}

Already here in this call surname, which is not attached to a ajax call is null even though data was entered there. The same is true for all the other fields. In my application I'm using Primefaces, JavaEE 1.6 with Wildfly.

What is the reason for that behavior? Is there anything I can change on my calls to prevent this?

like image 229
ipa Avatar asked Jun 23 '15 09:06

ipa


1 Answers

Why are fields reset after ajax update with Primefaces

This problem has 2 possible causes depending on the concrete functional requirement:

  1. It's because you didn't tell JSF to process the desired input values.

  2. Or, it's because you told JSF to also update the non-desired input values.

The solution depends on the cause:

  1. Add process="person" (or process="@form") to <p:ajax> to process the whole form.

  2. Or, edit update="person" to specify only the components which really need to be updated.

Given your complaint in the end of the question, I believe you're looking for #1.

See also:

  • Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
like image 96
BalusC Avatar answered Nov 01 '22 23:11

BalusC