In our Form, we are filling a second value, depending on the first value. When we submit the form, the ajax based value is lost.
- All fields are bound to a Hashmap, named
variables
- When
field1
is changed, we are using a<p:ajax>
handler to calculate the value offield2
.
form-snipped:
<p:inputText id="field1"
value="#{variables['field1']}" style="width:70px;"
styleClass="shortcut-dashboard">
<f:converter converterId="doubleConverter" />
<p:ajax event="change"
listener="#{someController.calculateField2}"
update=":form:field2"
process=":form:field1" />
</p:inputText>
<p:inputText id="field2" readonly="true"
value="#{variables['field2']}" style="width:70px;"
styleClass="shortcut-dashboard">
<f:converter converterId="doubleConverter" />
</p:inputText>
the calculateField2
-Method is updating the value field2
inside the hashmap, so when the ajax call is completed and field2
is updated, it shows the correct value.
//Example
public void calculateField2(){
this.variables.put(this.variables.get("field1") * 2);
}
- The controller that is holding the map and serving the callbacks is
viewscoped
(the new , working 2.2-viewscope, not the JSF 2.1 viewscope)
Now, when the form is submitted, something strange is happening inside the Apply Request Values Phase
: If other fields are not filled while required, the validation fails, and the form gets redisplayed. In this case:
- The value of
field1
is properly submitted and the corresponding field maintains it's value. - The value of
field2
however is submitted asnull
and therefore the field is empty again.
The Issue becomes "visible" when the validation fails. If all other fields are valid, the form submission is successfull but then storing null values inside the Update Model Values Phase
, thus the hashmap contains field2=null
I'm not sure where this value get's lost. To my understanding it shouldn't matter, whether i fill a textbox by hand, or using ajax. The "POST" should contain the values, and they should be applied to the textbox/model after submitting the form.
I figured out that the value is not commited, because it's field is set to readonly=true
.
Since this is a Primefaces-Component i'm not sure if this issue is related to JSFs validation (It's readonly, so it cannot change from null to anything) or behaviour of primefaces. (It's readonly, so no need to POST this value)
Tested with <h:inputText>
- same result. Readonly textfields are not submitted.
The solution provided by BalusC in the Post bellow is working so far. The only problem is that, after such an ajax call is submitted, the #{facesContext.renderResponse}
becomes false, thus the field is turned to readOnly false, after it has been modified by ajax.