3

I am making a form in front end in Magento 2 but sucked in this error anyone can please help me.
Below are my codes:

in app/code/My/Module/Block/Contact.php

<?php

namespace My\Module\Block;

class Contact extends \Magneto\Framework\View\Element\Template
{
/**
 * Construct
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    array $data = []
)
{
    parent::__construct($context, $data);
   }

/**
 * Get form action URL for POST booking request
 *
 * @return string
 */
public function getFormAction()
{
        // companymodule is given in routes.xml
        // controller_name is folder name inside controller folder
        // action is php file name inside above controller_name folder

    return '/mymodule/index/contact';
    // here controller_name is index, action is booking
}
}

in app/code/My/Module/Controller/index/Contact.php

<?php

namespace My\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Contact extends \Magento\Framework\App\Action\Action
{

/**
 * Contact action
 *
 * @return void
 */
public function execute()
{
    // 1. POST request : Get contact data
    $post = (array) $this->getRequest()->getPost();

    if (!empty($post)) {
        // Retrieve your form data
        $firstname   = $post['firstname'];
        $lastname    = $post['lastname'];
        $phone       = $post['phone'];
        $email = $post['email'];

        // Doing-something with...

        // Display the succes form validation message
        $this->messageManager->addSuccessMessage('Submmition done !');

        // Redirect to your form page (or anywhere you want...)
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl('/mymodule/index/stream_context_create()');

        return $resultRedirect;
    }
    // 2. GET request : Render the contact page 
    $this->_view->loadLayout();
    $this->_view->renderLayout();
}
}

in app/code/My/Module/etc/frontend/route.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                               xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
    <route id="mymodule" frontName="mymodule">
        <module name="My_Module"/>
    </route>
</router>
</config>

in app/code/My/Module/etc/module.xml

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

in app/code/My/Module/view/frontend/layout/mymodule_index_contact.xml

<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout /etc/page_configuration.xsd">
<head>
    <title>HTML title - The contact form page</title>
</head>
<body>
    <referenceBlock name="navigation.sections" remove="true" />
    <referenceContainer name="content">
        <block class="My\Module\Block\Contact" name="mymodule_contact"  template="My_Module::contact.phtml" />
    </referenceContainer>
</body>
</page>             

in app/code/My/Module/view/frontend/templates/contact.phtml

<h1>Contact page</h1>

<form action="<?php echo $block->getFormAction() ?>" method="post">
<input name="firstname" type="text">
<input name="lastname" type="text">
<input name="phone" type="text">
<input name="email" type="email">
<input type="submit" value="Send contact informations">
</form>

in app/code/My/Module/view/frontend/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'My_Module',
__DIR__
);

And the errors in the Browser are:

1 exception(s):
Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file:    'My_Module::contact.phtml' in module: 'My_Module' block's name: 'mymodule_contact'

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file:   'My_Module::contact.phtml' in module: 'My_Module' block's name: 'mymodule_contact'
<pre>#1 Magento\Framework\View\Element\Template->_toHtml() called at [lib/internal  /Magento/Framework/View/Element/AbstractBlock.php:668]
#2 Magento\Framework\View\Element\AbstractBlock->toHtml() called at [lib/internal/Magento/Framework/View/Layout.php:557]
#3 Magento\Framework\View\Layout->_renderBlock() called at [lib/internal/Magento/Framework/View/Layout.php:533]
#4 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#5 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#6 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#7 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#8 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#9 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#10 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#11 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#12 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#13 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#14 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#15 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#16 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#17 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#18 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#19 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#20 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at  [lib/internal/Magento/Framework/View/Layout.php:488]
#21 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#22 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#23 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#24 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#25 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#26 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#27 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#28 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#29 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#30 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#31 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#32 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:585]
#33 Magento\Framework\View\Layout->_renderContainer() called at [lib/internal/Magento/Framework/View/Layout.php:535]
#34 Magento\Framework\View\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
#35 Magento\Framework\View\Layout\Interceptor->renderNonCachedElement() called at [lib/internal/Magento/Framework/View/Layout.php:488]
#36 Magento\Framework\View\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
#37 Magento\Framework\View\Layout\Interceptor->renderElement() called at [lib/internal/Magento/Framework/View/Layout.php:954]
#38 Magento\Framework\View\Layout->getOutput() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:58]
#39 Magento\Framework\View\Layout\Interceptor->___callParent() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:138]
#40 Magento\Framework\View\Layout\Interceptor->Magento\Framework\Interception\{closure}() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:153]
#41 Magento\Framework\View\Layout\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:494]
#42 Magento\Framework\View\Layout\Interceptor->getOutput() called at [lib/internal/Magento/Framework/View/Result/Page.php:258]
#43 Magento\Framework\View\Result\Page->render() called at [lib/internal/Magento/Framework/View/Result/Layout.php:171]
#44 Magento\Framework\View\Result\Layout->renderResult() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:58]
#45 Magento\Framework\View\Result\Page\Interceptor->___callParent() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:138]
#46 Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception\{closure}() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:153]
#47 Magento\Framework\View\Result\Page\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/View/Result/Page/Interceptor.php:130]
#48 Magento\Framework\View\Result\Page\Interceptor->renderResult() called at [lib/internal/Magento/Framework/App/View.php:221]
#49 Magento\Framework\App\View->renderLayout() called at [app/code/My/Module/Controller/Index/Contact.php:40]
#50 My\Module\Controller\Index\Contact->execute() called at [generated/code/My/Module/Controller/Index/Contact/Interceptor.php:24]
#51 My\Module\Controller\Index\Contact\Interceptor->execute() called at [lib/internal/Magento/Framework/App/Action/Action.php:108]
#52 Magento\Framework\App\Action\Action->dispatch() called at [lib/internal/Magento /Framework/Interception/Interceptor.php:58]
#53 My\Module\Controller\Index\Contact\Interceptor->___callParent() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:138]
#54 My\Module\Controller\Index\Contact\Interceptor->Magento\Framework\Interception\{closure}() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:153]
#55 My\Module\Controller\Index\Contact\Interceptor->___callPlugins() called at [generated/code/My/Module/Controller/Index/Contact/Interceptor.php:39]
#56 My\Module\Controller\Index\Contact\Interceptor->dispatch() called at [lib/internal/Magento/Framework/App/FrontController.php:159]
#57 Magento\Framework\App\FrontController->processRequest() called at [lib/internal/Magento/Framework/App/FrontController.php:99]
#58 Magento\Framework\App\FrontController->dispatch() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:58]
#59 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:138]
#60 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [app/code/Magento/Store/App/FrontController/Plugin/RequestPreprocessor.php:94]
#61 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:135]
#62 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [app/code/Magento/PageCache/Model/App/FrontController/BuiltinPlugin.php:73]
#63 Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:135]
#64 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [lib/internal/Magento/Framework/Interception/Interceptor.php:153]
#65 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#66 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [lib/internal/Magento/Framework/App/Http.php:136]
#67 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#68 Magento\Framework\App\Http\Interceptor->launch() called at [lib/internal/Magento/Framework/App/Bootstrap.php:258]
#69 Magento\Framework\App\Bootstrap->run() called at [index.php:39]

  • which Magento version? – fmsthird Jun 14 at 10:18
1

The contact.phtml file should be inside view/frontend/templates directory but you have created templets directory instead of templates.

Please check the spelling of the templates directory.
It should be inside app/code/My/Module/view/frontend/templates/contact.phtml

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.