3 Replies Latest reply on Jan 24, 2009 7:48 AM by demetrio812

    Captcha and Ajax forms

    demetrio812 Apprentice

      Hi,
      I write here because no one answered in seamframework.org forum...

      I tried the example of the captcha and it worked very well in a normal form, now I have 2 problems:

      First: I'm implementing it in an ajax way using a:commandButton to submit the form and it seems it is not working after a lot of tries, here there is the code:

       <h:form id="nuovoCommentoFrm">
       <h:panelGrid columns="3">
       ...
       <h:outputText value="Control code*" />
       <h:panelGrid id="pgCaptcha" columns="1">
       <h:graphicImage value="/seam/resource/captcha?f=#{currentDate.time}" />
       <h:inputText id="verifyCaptcha" value="#{captcha.response}" required="true">
       <s:validate />
       </h:inputText>
       </h:panelGrid>
       <rich:message for="verifyCaptcha" errorClass="errorMessage"/>
       </h:panelGrid>
       <a:commandButton value="Add comment" action="#{articoloHelper.inserisciNuovoCommento}" reRender="pgCaptcha,nuovoCommentoFrm,frmCommenti" />
       </h:form>
      


      Am I wronging something?

      Second: even with the normal form, when the form submit is done all fields are cleared but the captcha.response one. I have seen it is a session object but I would like to clear it every time the submit is done (even from inside the submit method), is it possible?

      Thanks

      Demetrio

        • 1. Re: Captcha and Ajax forms
          Ilya Shaikovsky Master

          what did you mean by not working? action method not fired?

          second - yes you should clear the value yourself because old one will be shown anyway if value stored or if validation failed. and this is standard behavior. jsf inputs shows last value from submittedValue if validation fails.

          • 2. Re: Captcha and Ajax forms
            Alex Kolonitsky Newbie

            I've tried to reproduce it on the jboss-4.2.3.GA and haven't got any problem: the captcha worked fine with ajax request.

            If you wish, I can send test-application to you.

            • 3. Re: Captcha and Ajax forms
              demetrio812 Apprentice

              Hi!
              Thanks for the answers and sorry for my late one...

              It is working now I didn't know I had to clean it manually...

              I noticed that just setting the response was not enough so looking at the surce code I've found the you also have to call the init() method, so to clean the seam captcha the code is:

              @In
              Captcha captcha;
              
              ....
              
              captcha.setResponse(null);
              captcha.init();
              


              You can also call validateResponse with a wrong value so it will call init but it's better to directly call init I think.
              Also I think that instead of inject the captcha instance you can get using Captcha.instance().

              Am I missing something?

              Thanks again

              Demetrio