5

I am using PF3.5+JSF2.1.22 and in my web application i am using Primefaces Captcha component. I am getting some weird issue in capcha component,i used captcha component like this in application

<p:captcha id="captcha" label="Captcha" theme="white" />

And i have a PF command page to submit the values to bean

<p:commandButton id="clear" value="Clear" update="captcha" styleClass="kuberbutton" />

When i am using button like above after form submit if any validation issue and other issue coming and age is loading again then Captcha is not visible in page any more but when i am using ajax="false" in PF button then it is working,is this is behavior this component will work i have to do ajax="false"? I checked the PF website they also did same thing Primefaces Captcha

2 Answers2

8

Captcha component in Primefaces currently does not support ajax behavior , that why you must use ajax="false" in your <p:commandButton , you page must be fully reloaded for the captcha to work properly...


If you must have the ajax behavior you could use some other third party solution...


Haven't tried the following, but it might help with ajax issues:

recaptcha - AJAX AP

Displaying reCAPTCHA Without Plugins

How can I load a reCaptcha form using jQuery/AJAX while leaving the reCaptcha scripts in place?

Community
  • 1
  • 1
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Which third party solutions do you propose for ajax behavior? I would like to show recaptcha in a wizard dialog which is based on ajax requests and primefaces is definitly not a working solution for this problem. – Adrian Jul 29 '13 at 20:26
0

As already said Primefaces Captcha component can't be updated by ajax request. But there is a simple solution - update everything but not Captcha component itself.

Your XHTML:

<h:form id="myForm">
    <h:panelGroup id="updateFormAllValuesButNotCaptcha">
        Name: <p:inputText id="name" value="#{captchaBean.name}" required="true"/>
        <br/>
        Comment: <p:inputTextarea id="comment" value="#{captchaBean.comment}" required="true"/>
        <br/>
    </h:panelGroup>
    <p:captcha/>
    <p:commandButton value="click me" update="updateFormAllValuesButNotCaptcha"
         actionListener="#{captchaBean.someAction}" oncomplete="Recaptcha.reload()"
         onerror="Recaptcha.reload()"/>
</h:form>

<p:messages globalOnly="false" autoUpdate="true"/>

Your backing bean:

@ManagedBean
@ViewScoped
public class CaptchaBean implements Serializable {
    private String name;
    private String comment;

    public String getComment() { return comment; }

    public void setComment(String comment) { this.comment = comment; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public void someAction() {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Done", "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

Note that I am updating updateFormAllValuesButNotCaptcha panel which contains all form input fields but not Captcha itself. It is also important to notice that Captcha can't be reused, so you have to reload it always when ajax request has been completed or ended with error.

What you update after commandButton's action succeeded is up to you. You can hide form (do not render it) and show only confirmation message to make sure user won't try to send comment again.

wst
  • 4,040
  • 4
  • 41
  • 59