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)]
);
}
}