1

I am following this link https://magebug.blogspot.com/2018/05/magento-2-add-google-recaptcha-in.html

The link suggests add a controller, i have already a controller file, how to add more than one controller under execute()

2 Answers 2

2

Add script in file you want to use google Recaptcha

<script src="https://www.google.com/recaptcha/api.js" type="text/javascript" xml="space"></script>

Add captcha code to phtml or block/page where you want to add Recaptcha in the form

<div class="form-group required">   //change class as per themes
        <div class="g-recaptcha required" id="recaptcha"
             data-sitekey="your-site-key">
        </div>
             <div style="display:none" id="error-msg">
                  <span><?php echo __('This is a required field.') ?></span>
             </div>
</div>

If the captcha validation not working then add the below code:

<script>
    function checkCaptcha() {
        if ((jQuery('#g-recaptcha-response').val()) === '') {
            jQuery('#error-msg').css('display', 'block');
            jQuery('#error-msg').css('color', '#df280a');
            jQuery('#error-msg').css('font-size', 13);
            return false;
        }
        else {
            jQuery('#error-msg').css('display', 'none');
        }
    }
</script>

In form starting add onSubmit="return checkCaptcha() ;" event like:

<form class="form contact" action="<?php echo $block->getFormAction(); ?>" id="contact-form" method="post" onSubmit="return checkCaptcha() ;">
</form>
7
  • Still not working, can u pls help me, Code: codeshare.io/2BNrqn
    – zus
    Mar 18, 2021 at 4:25
  • Check how to create "data-sitekey" for reCaptcha and generate then use that generated data-sitekey here Mar 18, 2021 at 4:32
  • Yes i am using my sitekay.
    – zus
    Mar 18, 2021 at 4:33
  • You donot use any site key, first of all generate site key then add it in data-siteKey check this link developers.google.com/recaptcha/docs/display Mar 18, 2021 at 4:35
  • I am added my site : <div class="g-recaptcha required" id="recaptcha" data-sitekey="ipthjihyiohytihnijhthnthtrhrthtr0t">
    – zus
    Mar 18, 2021 at 4:36
1

You can do it using the following techniques: a) Create an observer for your customer form's controller, b) Utilize Magento\ReCapchaUi\Model\RequestHandlerInterface::execute() and use your custom request as key to handle the request.

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.