Small issue with simple recaptcha app

  • After a recent increase in spam submissions on a site, I have decided to ditch the honeypot method and create a simple app to validate a ReCaptcha v3 response in a form.


    All works apart from a test case when you submit a form successfully and then you refresh the page so the form submits again. The form throws a general perch error using $SubmittedForm->throw_error('spam', 'recaptcha'); because rightly so the g-recaptcha-response is now invalid as the response can not be used - but the form is successfully processed and stored by the forms app.


    The recaptcha field must be pulling the data through from the previous submission? Anyway debug and code below, any help appreciated - I did wonder whether it would make more sense to write a custom form validator instead and just add that to the recaptcha field.

    Debug output after page refreshed (form error along with the recaptcha response):

    My code

    In my form template I have the app attribute set on the form as app="boldr_recaptcha perch_forms" so that my app is passed the form first. I have a hidden input which is populated with the ReCaptcha javascript ready for submission <perch:input id="g-recaptcha-response" type="hidden" label="Recaptcha" />

    Snippets from my apps code:

  • marc

    Changed the title of the thread from “Form data being processed/stored twice?” to “Small issue with simple recaptcha app”.
  • drewm

    Approved the thread.
  • Hello marc,


    I think the response is processed independently by the apps listed in the app attribute.


    You can list your app app="boldr_recaptcha" without perch_forms and redispatch to the Forms app via your app form handler if Recaptcha is successful:


    Code
    1. $SubmittedForm->redispatch('perch_forms');


    This means no spam responses would be stored in Perch Forms though.

  • Thanks Hussein, that works.


    On a couple of forms on the site I had two apps in the forms apps="perch_forms boldr_subscribe" attribute prior to adding the recaptcha app.


    Just tried re-dispatching to both but I seem to get errors, I'm guessing it's not possible?


    Code
    1. [type] => 2
    2. [message] => call_user_func() expects parameter 1 to be a valid callback, function 'perch_forms boldr_subscribe_form_handler' not found or invalid function name
    3. [file] => /Users/Marc/Dropbox (Boldr)/Work/Chestnuts/Online/Website/Build/cms/core/lib/api/PerchAPI_SubmittedForm.class.php
    4. [line] => 452
  • Correct me if I am wrong but it appears you can add multiple instances of the re-dispatch function: