2

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');
    }

}
6
  • 'name' => 'filepath', this must be your database column name or have the value of file. Dec 14, 2015 at 6:04
  • Thank you for your reply. 'filepath' is the column name in database. Is there any rule for the value of this column?
    – Code Eater
    Dec 14, 2015 at 6:12
  • check the value are set for that just do the inspect elemet you will see a hidden <input type="hidden" value="your value" name="filepath[value]"> Dec 14, 2015 at 6:26
  • share your editAction code too.. Dec 14, 2015 at 6:32
  • are you trying to upload image or file? Dec 14, 2015 at 6:38

1 Answer 1

2

use this code instead for that for edit don't add validation classes.

$setting = array(
        'label'     => 'File',
        'name'      => 'filepath',
    );
    if (!Mage::registry('fileupload_data')->getId()){// if on "add" mode (use the same registry key as in your admin controller)
        $settings['required'] = true;
        $settings['class'] = 'required-entry';
    }
    $fieldset->addField('filepath', 'file', $setting);
7
  • Where to add the code? In editAction?
    – Code Eater
    Dec 14, 2015 at 6:49
  • in your save action where you upload the file use else statement if the use not upload the file. Dec 14, 2015 at 6:50
  • However, the input field is required. I cannot pass the validation.
    – Code Eater
    Dec 14, 2015 at 7:06
  • 'class' => 'required-entry required-file', add this class on file. Dec 14, 2015 at 7:16
  • It does not work. It still requires me to choose a file.
    – Code Eater
    Dec 14, 2015 at 8:09

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.