3

Trying to create simple form inside admin to perform some action inside Block class (included into layout) and return success or error message.

The thing is I needn't use any data source and edit/delete actions. So simply need to submit it and do some actions inside.

I tried to make form via template and layout, but submit takes me to "admin/dashboard" instead of needed controller whatever I tried to set inside form action parameter.

view\adminhtml\templates\index\index.phtml

<form class="form" action="" method="post">

    <fieldset class="fieldset">
        <legend class="legend"><span><?php echo __('Import Product') ?></span></legend><br>
        <div class="field required">
            <label for="number" class="label">
                <span><?php echo __('GTIN') ?></span>
            </label>

            <div class="control">
                <input type="text" id="number"
                       name="number"
                       class="input-text">
            </div>
        </div>
    </fieldset>

    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" class="action submit primary" title="<?php echo __('Submit') ?>">
                <span><?php echo __('Submit') ?></span>
            </button>
        </div>
    </div>

</form>

view\adminhtml\layout\importproduct_index_index.xml

<?xml version="1.0" ?>
<page layout="admin-1column" 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="Modules\Import\Block\Adminhtml\Index\Index" name="index.index"
                   template="Modules_Import::index/index.phtml"/>
        </referenceContainer>
    </body>
</page>

How to make submit to needed controller? Or is there another way to create simple form?

1

3 Answers 3

4

Sure that you missed to add form key:

<input name="form_key" type="hidden" value="<?php /* @escapeNotVerified */ echo $block->getFormKey() ?>" />

Remember to add Url action for your form.

Code for getFormKey() taken here vendor\magento\module-backend\Block\Template.php

3
  • 1
    Code for getFormKey() taken here vendor\magento\module-backend\Block\Template.php
    – white-imp
    Nov 7, 2016 at 16:27
  • @KhoaTruongDinh in my form, form key available and field's name attribute also available. Still, $this->getRequest()->getPostValue() return blank. Do you know solution about that? Nov 26, 2019 at 7:15
  • I added form in browser model dialogue. Same as like Catalog -> Categories -> Content -> Select From Gallery. Nov 26, 2019 at 7:15
2

For that you have to set your form action URL. It looks like you are using Import feature so you have to also add enctype="multipart/form-data"

In block file you can write

namespace Modules\Import\Block\Adminhtml\Index;

class Index extends \Magento\Backend\Block\Template
{

    protected $_backendUrl;

    public function __construct(
        \Magento\Backend\Model\UrlInterface $backendUrl
    ) {

        $this->_backendUrl = $backendUrl;
    }
    public function getPostUrl()
    {
        $params = array('some'=>'url_parameters');

        $url = $this->_backendUrl->getUrl("the/url/path", $params);
    }
}

And in your phtml file write this

<form class="form" action="<?php echo $block->getPostUrl(); ?>" method="post" enctype="multipart/form-data">
3
  • welcome back :-) Nov 7, 2016 at 16:47
  • @KeyurShah in my form, form key available and field's name attribute also available. Still, $this->getRequest()->getPostValue() return blank. Do you know solution about that? Nov 26, 2019 at 7:12
  • I added form in browser model dialogue. Same as like Catalog -> Categories -> Content -> Select From Gallery. Nov 26, 2019 at 7:14
0

Looks like you're missing an input with a form key!

I have had the same issue you're having multiple times.

Here's a way to get the form key:

<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

You'll also need to get the URL for your POST. For that you need to utilize the function getUrl()

Let me know if this helps ya!

Your Answer

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

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