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();

        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

        return parent::_prepareForm();





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)) {
        Mage::register('fileupload_data', $model);
    } else {
        Mage::getSingleton('adminhtml/session')->addError('File does not exist');

  • 'name' => 'filepath', this must be your database column name or have the value of file. Commented 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
    Commented 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]"> Commented Dec 14, 2015 at 6:26
  • share your editAction code too.. Commented Dec 14, 2015 at 6:32
  • are you trying to upload image or file? Commented Dec 14, 2015 at 6:38

1 Answer 1


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);
  • Where to add the code? In editAction?
    – Code Eater
    Commented 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. Commented Dec 14, 2015 at 6:50
  • However, the input field is required. I cannot pass the validation.
    – Code Eater
    Commented Dec 14, 2015 at 7:06
  • 'class' => 'required-entry required-file', add this class on file. Commented Dec 14, 2015 at 7:16
  • It does not work. It still requires me to choose a file.
    – Code Eater
    Commented Dec 14, 2015 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.