I am trying to retrive the upload file on form submit in admin grid.
In that I did not get the filepath because of the $model->getdata not able to find the path on line 123 in below code.
<?php
/**
* Webkul_Grid Add New Row Form Admin Block.
* @category Webkul
* @package Webkul_Grid
* @author Webkul Software Private Limited
*
*/
namespace Webkul\Grid\Block\Adminhtml\Grid\Edit;
/**
* Adminhtml Add New Row Form.
*/
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
/**
* @var \Magento\Store\Model\System\Store
*/
protected $_systemStore;
/**
* @param \Magento\Backend\Block\Template\Context $context,
* @param \Magento\Framework\Registry $registry,
* @param \Magento\Framework\Data\FormFactory $formFactory,
* @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
* @param \Webkul\Grid\Model\Status $options,
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
\Webkul\Grid\Model\Status $options,
array $data = []
) {
$this->_options = $options;
$this->_wysiwygConfig = $wysiwygConfig;
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Prepare form.
*
* @return $this
*/
protected function _prepareForm()
{
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$model = $this->_coreRegistry->registry('row_data');
$form = $this->_formFactory->create(
['data' => [
'id' => 'edit_form',
'enctype' => 'multipart/form-data',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
$form->setHtmlIdPrefix('shipment_');
$fieldset->addField(
'content',
'editor',
[
'name' => 'content',
'label' => __('Content'),
'style' => 'height:36em;',
'required' => true,
'config' => $wysiwygConfig
]
);
$fieldset->addField(
'publish_date',
'date',
[
'name' => 'publish_date',
'label' => __('Publish Date'),
'date_format' => $dateFormat,
'time_format' => 'H:mm:ss',
'class' => 'validate-date validate-date-range date-range-custom_theme-from',
'class' => 'required-entry',
'style' => 'width:200px',
]
);
$importdata_script = $fieldset->addField(
'importdata',
'file',
array(
'label' => 'Upload File',
'required' => true,
'name' => 'importdata',
'note' => 'Allow File type: .csv and .xls',
)
);
$importdata_script = $fieldset->setAfterElementHtml(
"
<script type=\"text/javascript\">
document.getElementById('grid_importdata').onchange = function () {
var fileInput = document.getElementById('grid_importdata');
var filePath = fileInput.value;
var allowedExtensions = /(\.csv|\.xls)$/i;
if(!allowedExtensions.exec(filePath))
{
alert('Please upload file having extensions .csv or .xls only.');
fileInput.value = '';
}
};
</script>"
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
– Milind Singh Aug 31 '20 at 15:26Upload: on upload of a file, this will save the in tmp and return the URL to UI Form
Save: on form save, this will receive the tmp file path which you can read and process values.