问题
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 "Vendor\Module\Controller\Index\Post\Interceptor" [] []
[2019-10-17 19:41:20] main.DEBUG: Request validation failed for action "Vendor\Module\Controller\Index\Post\Interceptor" [] []
[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 Vendor\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Post extends \Magento\Framework\App\Action\Action implements \Magento\Framework\App\Action\HttpPostActionInterface
{
/**
* 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 \Magento\Framework\DataObject();
$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 = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
->setTemplateOptions(
[
'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
'store' => \Magento\Store\Model\Store::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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/58439835/magento-2-debug-log-main-debug-request-validation-failed-for-action-vend