Steven De Groote
Steven De Groote

Reputation: 2233

Prevent ajax call via JS validation

In some cases, I need to do some simple client side validation, and depending on that, fire an ajax call or not. However, it appears that in IE, even when returning false in onchange, or in onstart on the p:ajax, the ajax event is still getting fired anyway.

So, my component currently looks like this:

    <p:inputText id="pc" value="#{bean.pc}" valueChangeListener="#{bean.doStuff}" styleClass="if-nospace-upper" onchange="return isOK()">
        <p:ajax immediate="true"  onstart="PF('busyPopup').show();" oncomplete="PF('busyPopup').hide();" />
    </p:inputText>

When invalid, my isOK() function returns false, but the ajax call is still being made, whereas I don't want it to.

Is there any possibility to do this at all?

Upvotes: 1

Views: 703

Answers (1)

techipank
techipank

Reputation: 430

One alternative is <p:remoteCommand>

    <p:inputText id="pc" value="#{bean.pc}" 
         valueChangeListener="#{bean.doStuff}" styleClass="if-nospace-upper" 
          onchange="return isOK()">
     </p:inputText>
     <p:remoteCommand name="rc">
          <p:ajax  onstart="PF('busyPopup').show();" oncomplete="PF('busyPopup').hide();" />
     </p:remoteCommand>

and from javascript :

funtion isOK(){
//If Condtion matches
rc();
}

and please take out immediate="true" noticed you are using for <p:ajax> although it is not giving you any problem here but it's not good to force jsf to skip some life cycle steps unless and untill you have requirement

Upvotes: 1

Related Questions