I am trying to create a form which consists of a file type field to import my data in csv file.
This is what I have done so far,
I tried simple form but my validations on the file type field were not working, so I rather thought of using tabs , thinking that it would work well but unfortunately tab was not getting displayed at all.
This is my code:
<?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="RB\CheckDelivery\Block\Adminhtml\Import\Edit" name="rb_zipcode_import"></block>
</referenceContainer>
<referenceContainer name="left">
<block class="RB\CheckDelivery\Block\Adminhtml\Import\Edit\Tabs" name="rb_zipcode_import_tabs">
<block class="RB\CheckDelivery\Block\Adminhtml\Import\Edit\Tab\Import" name="rb_zipcode_import_tab_main"/>
<action method="addTab">
<argument name="name" xsi:type="string">import_sections</argument>
<argument name="block" xsi:type="string">rb_zipcode_import_tab_main</argument>
</action>
</block>
</referenceContainer>
</body>
</page>
app/code/RB/CheckDelivery/Block/Adminhtml/Import/Edit/Tab/Import.php
namespace RB\CheckDelivery\Block\Adminhtml\Import\Edit\Tab;
class Import extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param \Magento\Store\Model\System\Store $systemStore
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Store\Model\System\Store $systemStore,
array $data = []
) {
$this->_systemStore = $systemStore;
parent::__construct($context, $registry, $formFactory,$data);
}
/**
* Prepare form
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareForm()
{
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('zipimports_');
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Import')]);
$fieldset->addField(
'zipcodes',
'file',
[
'name' => 'zipcodes',
'label' => __('Please choose a file to import'),
'required' => true
]
);
$this->setForm($form);
return parent::_prepareForm();
}
/**
* Prepare label for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabLabel()
{
return __('ZIP Code');
}
/**
* Prepare title for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabTitle()
{
return __('ZIP Code');
}
/**
* {@inheritdoc}
*/
public function canShowTab()
{
return true;
}
/**
* {@inheritdoc}
*/
public function isHidden()
{
return false;
}
}
app/code/RB/CheckDelivery/Block/Adminhtml/Import/Edit.php
namespace RB\CheckDelivery\Block\Adminhtml\Import;
class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
/**
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
) {
$this->_coreRegistry = $registry;
parent::__construct($context, $data);
}
/**
*
* @return void
*/
protected function _construct()
{
$this->_blockGroup = 'RB_CheckDelivery';
$this->_controller = 'adminhtml_import';
parent::_construct();
$this->removeButton('reset');
$this->removeButton('back');
$this->buttonList->update('save', 'label', __('Save Zipcodes'));
}
/**
* Get form save URL
*
* @see getFormActionUrl()
* @return string
*/
public function getSaveUrl()
{
return $this->getUrl('checkdelivery/*/saveImport', ['_current' => true,'active_tab' => '']);
}
}
app/code/RB/CheckDelivery/Block/Adminhtml/Import/Edit/Tabs.php
namespace RB\CheckDelivery\Block\Adminhtml\Import\Edit;
/**
* Admin page left menu
*/
class Tabs extends \Magento\Backend\Block\Widget\Tabs
{
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('import_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(__('Zipcode Information'));
}
}
Let me know what I am missing here of if there's an alternative way to achieve this.