6

I'm creating a custom form in Magento 2.3 admin.

Vendor/Module/view/adminhtml/layout/route_folder_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Adminhtml\NewFunction\Edit" name="newfunction.form.container" template="newfunction.phtml"/>
        </referenceContainer>
    </body>
</page>

Vendor/Module/Block/Adminhtml/NewFunction/Edit.php

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

/**
 * Export edit block
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
namespace Vendor\Module\Block\Adminhtml\NewFunction;

/**
 * @api
 * @since 100.0.2
 */
class Edit extends \Magento\Backend\Block\Template
{
    public function getNewFunctionUrl()
    {
        return $this->getUrl('route/folder/newfunction');
    }

}

Vendor/Module/view/adminhtml/templates/newfunction.phtml

<form action="<?php echo $block->getNewFunctionUrl()?>" method="post" enctype="multipart/form-data">
    <input type="text" name="hello"/>
    <input type="submit" value="Submit" />
</form>

Vendor/Module/Controller/Adminhtml/Folder/NewFunction.php

<?php

namespace Vendor\Module\Controller\Adminhtml\Folder;


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

    public function execute()
    {

        $params = $this->getRequest()->getParams();

    }
}

The form submits perfectly but when I debug the $params variable it shows only two parameters

key
adminhtml

I was expecting the hello text as one of the parameter but didn't receive one on form submit.

Is there anything wrong with the process or code?

  • $this->getRequest()->getPostValue(); – Rk Rathod Sep 19 at 7:14
  • getting blank array on running $this->getRequest()->getPostValue() – Rizwan Dhuka Sep 19 at 7:16
3

Your controller should extend Magento\Backend\App\Action and implements Magento\Framework\App\Action\HttpPostActionInterface, also don't forget about ACL.

So should look something like this:

namespace Vendor\Module\Controller\Adminhtml\Folder;

class NewFunction extends \Magento\Backend\App\Action implements \Magento\Framework\App\Action\HttpPostActionInterface
{
    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    public const ADMIN_RESOURCE = 'Magento_Cms::save';

    public function execute()
    {
        $params = $this->getRequest()->getParams();
        //OR
        $name = $this->getRequest()->getParam('hello'); 
    }
}

Also don't forget about form_key input, your view should look like:

<form action="<?php echo $block->getNewFunctionUrl()?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="form_key" value="<?= $block->getFormKey() ?>">
    <input type="text" name="hello"/>
    <input type="submit" value="Submit" />
</form>
  • If I use \Magento\Backend\App\Action as extends class then form submission navigates me to dashboard – Rizwan Dhuka Sep 19 at 8:04
  • @RizwanDhuka sorry, I updated the answer. Please don't forget about form_key in .phtml – underser Sep 19 at 8:07
  • Thanks @underser it worked! – Rizwan Dhuka Sep 19 at 9:01
0

You need to try code like this one.

<?php
namespace Vendor\Module\Controller\Adminhtml\Folder;

class NewFunction extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $params = $this->getRequest()->getParams();
        //OR
        $name = $this->getRequest()->getParam('hello'); 
    }
}
  • on executing this $params = $this->getRequest()->getParams(); I'm getting only two parameters as mentioned in the question and on executing this $name = $this->getRequest()->getParam('hello'); I'm getting NULL – Rizwan Dhuka Sep 19 at 7:18
  • Have you implement above code and run the caching command? remember you change extends class same as above I mension. – Dhiren Vasoya Sep 19 at 7:21
  • If I use \Magento\Backend\App\Action as extends class then form submission navigates me to dashboard. – Rizwan Dhuka Sep 19 at 8:03
  • after above change run the caching command and then check. – Dhiren Vasoya Sep 23 at 4:01

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.