Shishigami
Shishigami

Reputation: 451

<p:inputText> value not updated in model on change

I have a form that lets me edit a list of beans (one at a time), using buttons I can switch between the beans.

Keeping it simple :

public class MyBean {
   private String text;
}

public class MyController {
   private List<MyBean> availableBeans = new ArrayList<MyBean>(); // has five MyBeans with random text
   private MyBean selectedBean; // initialized with first element of list

   private int index = 0;

   public void nextBean() { index++; }
   public void previousBean() { index--; }

   private void refreshBean() { selectedBean = availableBeans.get(index); }
}

For the html part I have something like

<h:form id="someForm">
   <!-- stuff -->

    <p:inputText value="#{myController.selectedBean.text}" />

    <p:inplace editor="true" label="#{myController.selectedBean.text}" >
        <p:inputText value="#{myController.selectedBean.text}" />
    </p:inplace>

   <!-- more stuff-->
</h:form>

If I change the text inside the inplace tag, the variable in myBean will be updated just fine, but If I only use inputText the bean will still have the old value, even if I change it on the webpage. Why is that?

Upvotes: 1

Views: 17627

Answers (2)

Daniel
Daniel

Reputation: 37071

Its because the p:inplace editor="true" implicitly submits the value to the server while <p:inputText does not do it implicitly,

You can solve it in several ways

1) add submit button like <p:commandButton to submit the value from p:inputText

2) use p:ajax event="keyup" or event="change",inside p:inputText

also take a look at the showcase p:ajax enables ajax features on supported components.

p.s , remove the value attribute from the p:inplace (there is no such attribute in p:inplace)

Upvotes: 7

Manuel
Manuel

Reputation: 4258

Lets give your components ids:

<h:form id="someForm">
  <p:inputText id="first" value="#{myController.selectedBean.text}" />
  <p:inplace id="second" editor="true" value="#{myController.selectedBean.text}">
    <p:inputText id="third" value="#{myController.selectedBean.text}" />
  </p:inplace>
</h:form>
  1. According to the Primefaces Documentation 3.5 the component p:inplace has no attribute called value.

  2. Do you submit the form someForm when changing the value of first? Otherwise the updated values from first won't be passed to MyController and MyBean. p:inplace submits the values automatically whereby you have to do it yourself it you use the standard p:inputText.

Upvotes: 1

Related Questions