0

I have developed a custom module for displaying a contact form. On form submission, I am sending an email and redirecting back to the same page with a success message. The message was showing until I integrated a Google recaptcha as described here https://magently.com/blog/recaptcha-in-magento-custom-form/. After that the message is not showing. If I remove the captcha it works. I have added message using below code.

$this->messageManager->addSuccess('Email sent successfully');
$this->_redirect('pricequoteform/index');
2
  • Any issue in console after adding recaptcha? Dec 8, 2022 at 14:11
  • No. There is no error in console.
    – Akhilesh
    Dec 9, 2022 at 4:09

1 Answer 1

1

How do you put your logic in the Action controller when saving the Captcha response?

If it is controller then you are using the wrong strategy as messageManager is used in two terms:

  • via page refreshed

  • via a page load or an ajax call

So make sure you Google Recaptcha Action is using messageManager and then send the message.

like:

protected messageManager;

public function __construct(
    ...
    \Magento\Framework\Message\ManagerInterface $messageManager,
    ...
){
    $this->messageManager = $messageManager;
    ...
}

and then use:

$this->messageManager->addSuccess(__("Success"));

Or what you can do in Action controller is:

 public function execute() {
   $this->messageManager->addSuccess(__('Success!'));
   $redirect = $this->resultRedirectFactory->create();
   $redirect->setUrl('/captchaForm');
   return $redirect;

}

If it's good to you, that will be good meanwhile you can follow this extension also: https://github.com/mageplaza/magento-2-google-recaptcha may be it can be helpful to you.

2
  • I am new to Magento. Don't have any idea. I just followed the tutorial. It uses Magento's default Google ReCaptcha.
    – Akhilesh
    Dec 9, 2022 at 4:11
  • Used second method $redirect = $this->resultRedirectFactory->create();$redirect->setPath('pricequoteform/index'); and it worked. Thanks!
    – Akhilesh
    Dec 9, 2022 at 6:13

Your Answer

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

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