Magento 2 – Create Simple Admin Form with Submit and No Data Source

adminformmagento-2.1magento2

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?

Best Answer

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