cancel
Showing results for 
Search instead for 
Did you mean: 

Error to clone controller form contact

Error to clone controller form contact

hello I cloned the contact form controller to customize some fields but when sending data I get the following 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();
}

}
6 REPLIES 6

Re: Error to clone controller form contact

@jpenailillo 

Please add the code that you've used to override as well.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Error to clone controller form contact

How to cancel? This error appears when I send a POST to the controller with js. Sorry I'm new to this magento thing.

Re: Error to clone controller form contact

@jpenailillo 

You must have added code in the di.xml file to override.

Please describe all the files that you've created and post a code you've used for overriding.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Error to clone controller form contact

@Sanjay Jethva  this is the di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Contact\Controller\Index\Post" type="Esign\Contact\Controller\Magento\Contact\Index\Post" />
		
</config>

This is module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Esign_Contact" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Cms"/>
        </sequence>
	</module>
</config>

Re: Error to clone controller form contact

@jpenailillo 

https://drops.meetanshi.com/CQDIiw

There's no function like isEnabled on line number 64

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Error to clone controller form contact


@Sanjay Jethva wrote:

@jpenailillo 

You must have added code in the di.xml file to override.

Please describe all the files that you've created and post a code you've used for overriding.


Thanks for the update and quick reply. I'll be sure to keep an eye on this thread. Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution.