I created a form and it only contains a file upload field. Uploading new file works properly. However, when I want to edit the record, the file upload field shows "No file chosen", and when I go to save the record without choosing the file, it warns me to select a file since it is a required field. My question is how to put the value from database and prevent it from modification when no new file is chosen?
Thank you in advance.
Here is my code
class Company_Module_Block_Adminhtml_Fileupload_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
if(Mage::registry('fileupload_data')) {
$data = Mage::registry('fileupload_data')->getData();
}
$fieldset = $form->addFieldSet('fileupload_form', array('legend' => 'File Upload'));
if(isset($data['filepath']) && $data['filepath']) {
$fileName = $data['filepath'];
$data['filepath'] = 'directory'.'/'.$fileifName;
}
$fieldset->addField('filepath', 'file', array(
'label' => 'File',
'name' => 'filepath',
'index' => 'filepath',
'required' => true
));
$form->setValues($data);
return parent::_prepareForm();
}
}
Update:
editAction
public function editAction() {
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('model/files')->load($id);
if($model->getId() || $id == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if(!empty($data)) {
$model->setData($data);
}
Mage::register('fileupload_data', $model);
$this->loadLayout();
$this->_setActiveMenu('fileupload/fileupload');
$this->_addContent($this->getLayout()->createBlock('fileupload/adminhtml_fileupload_edit'))
->_addLeft($this->getLayout()->createBlock('fileupload/adminhtml_fileupload_edit_tabs'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError('File does not exist');
}
}
Best Answer
use this code instead for that for edit don't add validation classes.