Lester
Lester

Reputation: 1870

How to use <p:ajax> to pass javascript generated data to backing bean?

I have a js-function codeAddress() that processess the data from address and updates the values of fullAddress and validField.
I tried <p:ajax> to pass the data of fullAddress and validField to the backing bean, but the setter methods seem to get called one request delayed.

<h:form id="addressForm">
    <p:inputText id="address">
        <p:ajax onstart="codeAddress()" process="fullAddress validField"/>
    </p:inputText>
    <p:commandButton value="submit" />
    <p:inputText id="fullAddress" value="#{addressBean.fullAddress}" />
    <p:inputText id="validField" value="#{addressBean.valid}" />
</h:form>

Upvotes: 0

Views: 794

Answers (1)

BalusC
BalusC

Reputation: 1109635

The onstart is invoked right before the ajax request is about to be sent. At that moment the ajax request is already prepared for long. It's thus too late to let it to take the changed input values into account.

Better use the input component's onchange attribute instead. It's invoked before the ajax request is to be prepared.

<p:inputText id="address" onchange="codeAddress()">
    <p:ajax process="fullAddress validField"/>
</p:inputText>

Upvotes: 2

Related Questions