1

You need to do a captcha validation so that the data is not saved in the database if the captcha is not correct. Here is the controller where the form is saved and validated.

Ronis\Feedback\Controller\Index\Post.php

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Ronis\Feedback\Model\ConfigInterface;
use Ronis\Feedback\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

class Post extends \Ronis\Feedback\Controller\Index implements HttpPostActionInterface
{
    protected $httpHeader;
    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @var Context
     */
    private $context;

    /**
     * @var MailInterface
     */
    private $mail;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param \Magento\Framework\HTTP\Header $httpHeader,
     * @param Context $context
     * @param ConfigInterface $feedbackConfig
     * @param MailInterface $mail
     * @param DataPersistorInterface $dataPersistor
     * @param LoggerInterface $logger
     */

    public function __construct(
        Context $context,
        \Magento\Framework\HTTP\Header $httpHeader,
        ConfigInterface $feedbackConfig,
        MailInterface $mail,
        DataPersistorInterface $dataPersistor,
        LoggerInterface $logger = null
    ) {
        parent::__construct($context, $feedbackConfig);
        $this->httpHeader = $httpHeader;
        $this->context = $context;
        $this->mail = $mail;
        $this->dataPersistor = $dataPersistor;
        $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
    }

    /**
     * Post user question
     *
     * @return Redirect
     */
    public function execute()
    {
        if (!$this->getRequest()->isPost()) {
            return $this->resultRedirectFactory->create()->setPath('*/*/');
        }
        try {
            $this->addDataBd($this->validatedParams());
            $this->sendEmail($this->validatedParams());
            $this->messageManager->addSuccessMessage(
                __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
            );
            $this->dataPersistor->clear('feedback');
        } catch (LocalizedException $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
            $this->dataPersistor->set('feedback', $this->getRequest()->getParams());
        } catch (\Exception $e) {
            $this->logger->critical($e);
            $this->messageManager->addErrorMessage(
                __('An error occurred while processing your form. Please try again later.')
            );
            $this->dataPersistor->set('feedback', $this->getRequest()->getParams());
        }
        return $this->resultRedirectFactory->create()->setPath('feedback/index');
    }

    //6LcRJZcUAAAAAA0_SbrE_fHW1zbaJ3JnpMlwUwkt - site

    //6LcRJZcUAAAAALfF-aLq9804zowngpLlcu_Qi1-P - secret
    /**
     * @return array
     * @throws \Exception
     */
    private function validatedParams()
    {
        $request = $this->getRequest();
        if (trim($request->getParam('feedback_name')) === '') {
            throw new LocalizedException(__('Enter the Name and try again.'));
        }
        if (trim($request->getParam('feedback_content')) === '') {
            throw new LocalizedException(__('Enter the comment and try again.'));
        }
        if (trim($request->getParam('feedback_subject')) === '') {
            throw new LocalizedException(__('Enter the subject and try again.'));
        }
        if (false === \strpos($request->getParam('feedback_email'), '@')) {
            throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
        }

        return $request->getParams();
    }
    private function addDataBd(){
        $objctManager = \Magento\Framework\App\ObjectManager::getInstance();

        $feedback = $objctManager->create('Ronis\Feedback\Model\Feedback');

        $name = $this->getRequest()->getPost('feedback_name');
        $email = $this->getRequest()->getPost('feedback_email');
        $phone = $this->getRequest()->getPost('feedback_telephone');
        $userAgent = $this->httpHeader->getHttpUserAgent();
        $remote = $objctManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress'); //remote ip
        $subject = $this->getRequest()->getPost('feedback_subject');
        $other_subject = $this->getRequest()->getPost('other_subject');
        $message = $this->getRequest()->getPost('feedback_content');

        $feedback->setData('feedback_name', $name);
        $feedback->setData('feedback_email', $email);
        $feedback->setData('feedback_telephone', $phone);
        if($subject == 'other'){
            $feedback->setData('feedback_subject', $other_subject);
        }
        else {
        $feedback->setData('feedback_subject', $subject);
        }
        $feedback->setData('feedback_content', $message);
        $feedback->setData('is_active', 0);
        $feedback->setData('feedback_user_agent', $userAgent);
        $feedback->setData('feedback_remote_ip', $remote->getRemoteAddress());
        $feedback->save();
    }

    /**
     * @param array $post Post data from feedback form
     * @return void
     */
    private function sendEmail($post)
    {
        $this->mail->send(
            $post['feedback_email'],
            ['data' => new DataObject($post)]
        );
    }

}
0

Magento 2 already has extension for google captcha. You can check the below post from magento.

https://docs.magento.com/m2/ce/user_guide/magento/extension-install-google-recaptcha.html

You can extend that module in your layout file to add the captcha in your form which will solve your problem.

  • 1
    Magento provide google default captcha in Higher than Magento version 2.3.x – Chirag Patel Mar 20 at 10:26
  • Not older version of 2.3.x – Chirag Patel Mar 20 at 10:26
  • So there you can choose only basic forms, and how can I do for my custom form that I use in the module – Рома Лытарь Mar 20 at 10:31
  • you can extend the module functionality to your module and show the necessary template in your layout file. And you have check @ChiragPatel answer also. It supports only m2.3.x version. – Thamo Mar 20 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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