I’m implementing a Custom Module that essentially has the same functionality as the default Magento 2 Contact Form. I’m receiving a very irritating error I can’t seem to find a solution to. It seems my POST request is failing Magento’s request validation, but I’m not sure how to find any more specific information.
I’ve attempted to implement a “CSRFSkip” plugin that bypasses the Magento CSRF validation. I’ve tried changing what my controller extends and implements. I’ve searched the logs extensively.
Here’s my Magento debug.log as the error is happening.
[2019-10-17 19:39:16] main.DEBUG: Request validation failed for action "VendorModuleControllerIndexPostInterceptor" [] []
[2019-10-17 19:41:20] main.DEBUG: Request validation failed for action "VendorModuleControllerIndexPostInterceptor" [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'catalog.compare.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'sale.reorder.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'wishlist_sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
Here’s my controller. /Vendor/Module/Controller/Index/Post.php
<?php
namespace VendorModuleControllerIndex;
use MagentoFrameworkControllerResultFactory;
class Post extends MagentoFrameworkAppActionAction implements MagentoFrameworkAppActionHttpPostActionInterface
{
/**
* Post action
*
* @return void
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
if (!$post['name']) {
$this->_redirect('/find-a-dealer');
return;
}
$this->inlineTranslation->suspend();
try {
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$this->logger->debug($post['name']);
if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
$error = true;
}
$this->logger->debug($post['email']);
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
$this->logger->debug($post['city']);
if (!Zend_Validate::is(trim($post['city']), 'NotEmpty')) {
$error = true;
}
$this->logger->debug($post['state']);
if (!Zend_Validate::is(trim($post['state']), 'NotEmpty')) {
$error = true;
}
$this->logger->debug($post['zip']);
if (!Zend_Validate::is(trim($post['zip']), 'NotEmpty')) {
$error = true;
}
$this->logger->debug($post['part']);
if (!Zend_Validate::is(trim($post['part']), 'NotEmpty')) {
$error = true;
}
if ($error) {
throw new Exception();
}
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
->setTemplateOptions(
[
'area' => MagentoBackendAppAreaFrontNameResolver::AREA_CODE,
'store' => MagentoStoreModelStore::DEFAULT_STORE_ID,
]
)
->setTemplateVars(['data' => $postObject])
->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->setReplyTo($post['email'])
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
$this->messageManager->addSuccess(
__('Thanks for contacting us with your comments and questions. We'll respond to you very soon.')
);
$this->getDataPersistor()->clear('find-a-dealer');
$this->_redirect('find-a-dealer');
return;
} catch (Exception $e) {
$this->inlineTranslation->resume();
$this->messageManager->addError(
__('We can't process your request right now. Sorry, that's all we know.')
);
$this->getDataPersistor()->set('find-a-dealer', $post);
$this->_redirect('find-a-dealer');
return;
}
}
/**
* Get Data Persistor
*
* @return DataPersistorInterface
*/
private function getDataPersistor()
{
if ($this->dataPersistor === null) {
$this->dataPersistor = ObjectManager::getInstance()
->get(DataPersistorInterface::class);
}
return $this->dataPersistor;
}
}
I expect for my post function to run and dispatch my email.
What actually happens is a redirect to my form page, and some validation related errors in the logs.
3
Answers
Can you check the permissions for var, generated and pub folders? When using root user for deploying changes the permissions can change to 644 causing these type of issues.
Don’t call the url from browser directly.
add it to a menu action link or to a button and call it from there.
OR
Disable the key in the admin and try.
In my case, it is the form_key. I’ve added form key input field explicitly in and it solved the issue.