0

We are using Google ReCaptcha for signup forms. We have created some more custom forms for which we would like to exclude the validation. Currently the re-catpcha is not showing in the form but on submit it gives error message "Invalid ReCaptcha validation"

The form is added as a CMS page where it includes the {{block}}

1 Answer 1

0

I override the ReCaptcha observer using Virtual type class

In di.xml added the following:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <virtualType name="MSP\ReCaptcha\Observer\Frontend\CreateUserObserver"
                 type="Vendor\Module\Observer\ReCaptchaObserver">
        <arguments>
            <argument name="isCheckRequired"
                      xsi:type="object">MSP\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\CreateUser</argument>
            <argument name="failureProvider"
                      xsi:type="object">MSP\ReCaptcha\Model\Provider\Failure\CreateUserObserver</argument>
        </arguments>
    </virtualType>
</config>

And then in the observer file, using a custom hidden input of the form, returned true.

<?php

namespace Iksula\EmployeeSignup\Observer;

use \Magento\Framework\App\Action\Action;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use \MSP\ReCaptcha\Api\ValidateInterface;
use \MSP\ReCaptcha\Model\IsCheckRequiredInterface;
use \MSP\ReCaptcha\Model\Provider\FailureProviderInterface;
use \MSP\ReCaptcha\Model\Provider\ResponseProviderInterface;
use \Magento\Framework\App\RequestInterface;
 
class ReCaptchaObserver implements ObserverInterface
{
    public function __construct(
        ResponseProviderInterface $responseProvider,
        ValidateInterface $validate,
        FailureProviderInterface $failureProvider,
        RemoteAddress $remoteAddress,
        IsCheckRequiredInterface $isCheckRequired,
        RequestInterface $request
    ) {
        $this->responseProvider = $responseProvider;
        $this->validate = $validate;
        $this->failureProvider = $failureProvider;
        $this->remoteAddress = $remoteAddress;
        $this->isCheckRequired = $isCheckRequired;
        $this->_request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $form_data = $this->_request->getParams();
        if (array_key_exists('custom_form_input_key', $form_data)) {
            return true;
        } else {
            if ($this->isCheckRequired->execute()) {
                $reCaptchaResponse = $this->responseProvider->execute();
                $remoteIp = $this->remoteAddress->getRemoteAddress();
    
                /** @var Action $controller */
                $controller = $observer->getControllerAction();
    
                if (!$this->validate->validate($reCaptchaResponse, $remoteIp)) {
                    $this->failureProvider->execute($controller ? $controller->getResponse() : null);
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.