0

hello I cloned the contact form controller to customize some fields but when sending data I get the following error, I think it is due to the constructor:

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Esign\Contact\Controller\Magento\Contact\Index\Post\Interceptor

2
  • 1
    please share full code
    – Amit Bera
    Sep 29, 2021 at 3:58
  • Please share the code which you have do. Sep 30, 2021 at 4:55

1 Answer 1

0

Hi now i have this error:

Fatal error: Uncaught Error: Call to a member function isEnabled() on null in /var/www/html/app/code/Magento/Contact/Controller/Index.php:64 Stack trace:

<?php

namespace Esign\Contact\Controller\Magento\Contact\Index;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; 
use Magento\Contact\Model\ConfigInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\Controller\ResultFactory;
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 \Magento\Contact\Controller\Index\Post
{

/**
 * @var DataPersistorInterface
 */
private $dataPersistor;

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

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

/**
 * @var MailInterface
 */
private $mail;
/**
 * @param Context $context
 * @param ConfigInterface $contactsConfig
 * @param DataPersistorInterface $dataPersistor
 * @param LoggerInterface $logger
 */
public function __construct(
    Context $context,
    ConfigInterface $contactsConfig,
    MailInterface $mail,
    DataPersistorInterface $dataPersistor,
    LoggerInterface $logger = null
) {
    
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    $this->context = $context;
    $this->mail = $mail;
    $this->dataPersistor = $dataPersistor;
    $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

public function execute()
{
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    if (!$this->getRequest()->isPost()) {
        return $this->resultRedirectFactory->create()->setPath('*/*/');
    }
    try {
        $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('contact_us');
    } catch (LocalizedException $e) {
        $this->messageManager->addErrorMessage($e->getMessage());
        $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
    } catch (\Exception $e) {
        $this->messageManager->addErrorMessage(
            __('An error occurred while processing your form. Please try again later.')
        );
        //$this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
    }
}

/**
 * @param array $post Post data from contact form
 * @return void
 */
private function sendEmail($post)
{
    $this->mail->send(
        $post['email'],
        ['data' => new DataObject($post)]
    );
}
/**
 * @return array
 * @throws \Exception
 */
private function validatedParams()
{
    $request = $this->getRequest();
    if (trim($request->getParam('name')) === '') {
        throw new LocalizedException(__('Enter the Name and try again.'));
    }
    if (trim($request->getParam('comment')) === '') {
        throw new LocalizedException(__('Enter the comment and try again.'));
    }
    if (false === \strpos($request->getParam('email'), '@')) {
        throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
    }
    if (trim($request->getParam('hideit')) !== '') {
        throw new \Exception();
    }

    return $request->getParams();
}

}
1
  • This should have just been an edit to your original post.
    – Brett
    Oct 2, 2021 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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