Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckContactUsFormObserver.php
Go to the documentation of this file.
1 <?php
7 
11 
13 {
17  protected $_helper;
18 
22  protected $_actionFlag;
23 
27  protected $messageManager;
28 
32  protected $redirect;
33 
38 
42  private $dataPersistor;
43 
51  public function __construct(
52  \Magento\Captcha\Helper\Data $helper,
53  \Magento\Framework\App\ActionFlag $actionFlag,
54  \Magento\Framework\Message\ManagerInterface $messageManager,
55  \Magento\Framework\App\Response\RedirectInterface $redirect,
57  ) {
58  $this->_helper = $helper;
59  $this->_actionFlag = $actionFlag;
60  $this->messageManager = $messageManager;
61  $this->redirect = $redirect;
62  $this->captchaStringResolver = $captchaStringResolver;
63  }
64 
71  public function execute(\Magento\Framework\Event\Observer $observer)
72  {
73  $formId = 'contact_us';
74  $captcha = $this->_helper->getCaptcha($formId);
75  if ($captcha->isRequired()) {
77  $controller = $observer->getControllerAction();
78  if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
79  $this->messageManager->addError(__('Incorrect CAPTCHA.'));
80  $this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
81  $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
82  $this->redirect->redirect($controller->getResponse(), 'contact/index/index');
83  }
84  }
85  }
86 
92  private function getDataPersistor()
93  {
94  if ($this->dataPersistor === null) {
95  $this->dataPersistor = ObjectManager::getInstance()
96  ->get(DataPersistorInterface::class);
97  }
98 
99  return $this->dataPersistor;
100  }
101 }
$helper
Definition: iframe.phtml:13
__construct(\Magento\Captcha\Helper\Data $helper, \Magento\Framework\App\ActionFlag $actionFlag, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\Response\RedirectInterface $redirect, CaptchaStringResolver $captchaStringResolver)
__()
Definition: __.php:13
$captcha
Definition: default.phtml:12
$controller
Definition: info.phtml:14