Danijel
Danijel

Reputation: 8610

JSF `action` not executed if javascript confirmation box used?

I am using jsf PrimeFaces (v 3.5.0) library. The following code doesn't execute delete after javascript confirmation box OK button is selected.

<p:commandLink id="deletePGLinkId"
    action="#{PresetGroupMgmtBean.delete}"
    onclick="return confirm('Preset Group will be removed. Are you sure you want to continue?');"
    update=":pmForm:presetPanel :pmForm:messagePanel">
      <f:param value="#{item.value.ID}" name="deleteID"></f:param>
      <h:graphicImage alt="Delete Image" style="border: none" value="./images/x.png" />  
</p:commandLink>

I'd like to avoid using PrimeFaces ConfirmDialog since I have so many confirmations, and it will take time to write a ConfirmDialog for each. Javascript box seems easy, but it doesn't work.

Why doesn't it execute delete?

Upvotes: 2

Views: 2097

Answers (1)

BalusC
BalusC

Reputation: 1109262

This is somewhat a design bug in PrimeFaces command link renderer. Look closer in the generated HTML output:

onclick="return confirm('Sure?');;PrimeFaces.ab({source:'formId:linkId'});return false;"

Do you see it now? The PrimeFaces.ab() who's responsible for submitting the form will this way never be invoked! You'd need to write the condition differently:

onclick="if (!confirm('Sure?')) return false;"

this way the HTML will ultimately be generated like follows:

onclick="if (!confirm('Sure?')) return false;;PrimeFaces.ab({source:'formId:linkId'});return false;"

this way it will properly continue to PrimeFaces.ab() when confirm() has returned true (it will then not invoke the return false; statement).

Mojarra uses jsf.util.chain() for this. Replace <p:commandLink> by <h:commandLink> and you'll see it in the generated HTML output:

onclick="jsf.util.chain(this,event,'return confirm(\'Sure?\');','mojarra.jsfcljs(document.getElementById(\'formId\'),{\'formId:linkId\':\'formId:linkId\'},\'\')');return false"

This way your initial construct would have worked.

Upvotes: 3

Related Questions