0
<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace task\Mycontact\Controller\Index;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
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\Framework\App\Action\Action implements HttpPostActionInterface
{
    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

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


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

    private $mycontactFactory;

    /**
     * @param Context $context
     * @param ConfigInterface $contactsConfig
     * @param MailInterface $mail
     * @param DataPersistorInterface $dataPersistor
     * @param LoggerInterface $logger
     */
    public function __construct(
        Context $context,
        DataPersistorInterface $dataPersistor,
        \Sigma\Mycontact\Model\MyContactFactory $mycontactFactory,
        LoggerInterface $logger = null
    ) {
        parent::__construct($context);
        $this->context = $context;
        $this->dataPersistor = $dataPersistor;
        $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
        $this->mycontactFactory = $mycontactFactory;
    }

    /**
     * Post user question
     *
     * @return Redirect
     */
    public function execute()
    {
//        $data = $this->getRequest()->getParams();
//        print_r($data);
//        exit();
        if (!$this->getRequest()->isPost()) {
            return $this->resultRedirectFactory->create()->setPath('*/*/');
        }
        try {
//             $this->validatedParams();
//            $this->validatedParams();
//            $request = $this->getRequest();
//            $data = $request->getParams();
//            print_r($data);
//            exit();

             $request = $this->getRequest();

             $data = $request->getParams();

             $model =$this->MyContactFactory->create();
            echo "hi";
             $model->setData($data);
             $model->save();
            $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->logger->critical($e);
            $this->messageManager->addErrorMessage(
                __('An error occurred while processing your form. Please try again later.')
            );
            $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
        }
        return $this->resultRedirectFactory->create()->setPath('mycontactform/index');
    }


    /**
     * @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();
//    }
}

main.CRITICAL: Notice: Undefined property: task\Mycontact\Controller\Index\Post\Interceptor::$MyContactFactory in /var/www/html/magento23/app/code/task/Mycontact/Controller/Index/Post.php on line 84 {"exception":"[object] (Exception(code: 0): Notice: Undefined property:

0

You are using the wrong variable in execute function. MyContactFactory start with the small case as like below.

$model = $this->myContactFactory->create();
5
  • after that again I got this exception
    – Sampath
    Dec 17 '20 at 7:31
  • [2020-12-17 07:29:16] main.CRITICAL: Class task\Mycontact\Model\MyContact does not exist {"exception":"[object] (ReflectionException(code: -1): Class task\\Mycontact\\Model\\MyContact does not exist at /var/www/html/magento23/vendor/magento/framework/Code/Reader/ClassReader.php:19)"} []
    – Sampath
    Dec 17 '20 at 7:31
  • @Magento2 either task\Mycontact\Model\MyContact class does not exit or there is some error in this class. Ans also ideally class name should start with upprcase, it should be Task not task. Dec 17 '20 at 7:45
  • <?php namespace Task\Mycontact\Model; class Mycontact extends \Magento\Framework\Model\AbstractModel { protected function _construct() { $this->_init('Task\Mycontact\Model\ResourceModel\Mycontact'); } // public function getAvailableStatuses() // { // // // $availableOptions = ['1' => 'Enable', // '0' => 'Disable']; // // return $availableOptions; // } }
    – Sampath
    Dec 17 '20 at 8:32
  • that is my Mycontact class
    – Sampath
    Dec 17 '20 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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