Forum

Thread tagged as: Question

Integrate Google Recaptcha with Perch forms

Hi all,

Has anyone successfully integrated Google's ReCaptcha to Perch forms? I tried looking at validation helpers, but these seem to be tied in to perch:inputs and I wasnt sure how to validate against the recaptcha's 'post'. I've seen various people ask the question in this forum, but most are told that it's not required and that it's not user friendly etc and seem to be shot down pretty quick.

Regardless of what other people think - is is possible to use the Google Recaptcha with Perch Forms using a validation helper so that the option is there for people to use should they wish to?

Jon Young

Jon Young 0 points

  • 3 years ago

I have recently been looking to do this and can't figure it out. I would like to know this too, please.

This is something I've built for a client (unfortunately I'm not able to provide any source code due to this). The app works by being used in place of perch_forms in the opening perch:form tag. The ReCaptcha PHP library validates the request and decides whether to throw a form_error response or to re-dispatch the request onto the forms app:

if ($this->validator->isValid()) {
            if (function_exists('perch_forms_form_handler')) {
                $formData->redispatch('perch_forms');
            }
        }

The snippet above is from the runtime form handler code that passes the form data along.

Using the Perch API you can add settings to allow you to store the key within the admin panel and the feathers API to automatically include the javascript API. For the client app there's a template handler letting you shortcut the code with perch:recaptcha tags. I'm not sure its possible using a validator as I believe these only pass the input string along, not the entire request.

Is there any way a custom fieldtype can be added to render a google recaptcha field in the same way the google map field type has been created? I appreciate your response James, but without a more fleshed out example of your solution, I'm not really sure what to do with it.

You're quite right, I worried that without being able to post source code it might not be as helpful as I'd hoped. I'll try to flesh it out a little more. The app passes the form response to the php recaptcha library from google. Perch submits this data to a form handler function which can then execute the validation function on the recaptcha library. If this check returns successful the response is then sent onto the official forms app to be stored / validated as normal, otherwise it runs the form_error() method from perch to stop the request and show any error messages to the user.

I've written a quick form handler example here which would go into a custom apps runtime.php

function your_app_form_handler($form) {

    $data = $form->data;
    $responseKey = null;

    // This is what the widget returns when submitting the form
    if(isset($data['g-recaptcha-response']) {
        $responseKey = $data['g-recaptcha-response'];
    }

    $recaptcha = new \ReCaptcha\ReCaptcha('secret_api_key');
    $recaptcha->validate($responseKey, $_SERVER['REMOTE_ADDR']);

    if($recaptcha->isValid() {
        $form->redispatch('perch_forms'); // send along
    } else {
        $form->throw_error('spam', 'recaptcha'); // stop and alert user
    }

}

Within the form template you can include the front end widget parts from Google's documentation. I'm not sure if a custom field type would work in a user facing form template.

Thanks for your time James, and for your response - I'll see if I can get this working. :)

Jon. No a field type won’t work on a public facing form, they are strictly for admin and runtime only.