0

I am creating a custom magento module for the backend part, everything is working fine so far, except the fact that when I want to edit an item, on the edit page the form is not populated with values. I did not manage to find a working solution online, hope someone can help me.

This is my Mymodule/Wine/Block/Adminhtml/Wine/Edit/Tab/Form

<?php

class Mymodule_Wine_Block_Adminhtml_Wine_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('wine_form', array('legend' => Mage::helper('wine')->__('Upload .xls Settings')));

        $fieldset->addField('title', 'text', array(
            'label'     => Mage::helper('wine')->__('TItle'),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'title',
        ));

        $fieldset->addField('filename', 'file', array(
            'label'     => Mage::helper('wine')->__('File .xls '),
            'required'  => true,
            'name'      => 'file'
        ));

        if (Mage::getSingleton('adminhtml/session')->getWineData()) {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getWineData());
            Mage::getSingleton('adminhtml/session')->setWineData(null);
        } elseif (Mage::registry('wine_data')) {
            $form->setValues(Mage::registry('wine_data')->getData());
        }
        return parent::_prepareForm();
    }
}

The edit and save actions in Mymodule/Wine/controllers/Adminhtml/WineController.php

public function editAction()
    {
        $wineId      = $this->getRequest()->getParam('id');
        $wineModel = Mage::getModel('wine/wine')->load($wineId);

        if ($wineModel->getId() || $wineId == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getWineData(true);

            Mage::register('wine_data', $wineModel);

            $this->loadLayout();
            $this->_setActiveMenu('wine/items');
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('.xls Manager'), Mage::helper('adminhtml')->__('.xls Manager'));
            // $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Managerxxx'), Mage::helper('adminhtml')->__('Item Managerxxx'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

            $this->_addContent($this->getLayout()->createBlock('wine/adminhtml_wine_edit'))
                  ->_addLeft($this->getLayout()->createBlock('wine/adminhtml_wine_edit_tabs'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('wine')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function saveAction()
    {
        if ($postData = $this->getRequest()->getPost()) {
            try {
                $wineModel = Mage::getModel('wine/wine');
                $fileName  = '';

                if (isset($_FILES['file']['filename']) && $_FILES['file']['filename'] != '') {
                    try {
                        $path       = Mage::getBaseDir('media') . DS . 'xls' . DS;
                        $fileName= $_FILES['file']['filename'];
                        $uploader = new Varien_File_Uploader('file');
                        $uploader->setAllowedExtensions(array('XLS', 'xls', 'TXT', 'txt'));
                        $uploader->setAllowCreateFolders(true);
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $uploader->save($path, $fileName);
                    } catch (Exception $e) {
                        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('wine')->__($fileName . ' Invalid file format'));
                        $this->_redirect('*/*/');
                        return;
                    }
                }

                $wineModel->setId($this->getRequest()->getParam('id'))
                            ->setName($postData['title'])
                            ->setStatus(0) // $postData['status']P
                            ->setCreated_at(now())
                            ->setExecuted_ad(now())
                            ->setFilename($fileName)
                            ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setWineData(false);
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setWineData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

1 Answer 1

0

Try this:

<?php

class Mymodule_Wine_Block_Adminhtml_Wine_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $fieldset = $form->addFieldset('wine_form', array('legend' => Mage::helper('wine')->__('Upload .xls Settings')));

        $fieldset->addField('title', 'text', array(
            'label'     => $this->__('TItle'),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'title',
        ));

        $fieldset->addField('filename', 'file', array(
            'label'     => $this->__('File .xls '),
            'required'  => true,
            'name'      => 'file'
        ));

        if ($data = Mage::getSingleton('adminhtml/session')->getWineData(false)) {
            $form->setValues($data);
        } elseif (Mage::registry('wine_data')) {
            $form->setValues(Mage::registry('wine_data')->getData());
        }
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

I have also simplified some of your code down bottom as well. If your form still doesn't prepopulate, then you need to dump your two data sources and make sure that title and file actually exist and contain a value.

1
  • It works with your code, and with mine also, it was just that the index names were different. You have to be really careful in magento. Thanks for the answer. Commented Sep 30, 2016 at 7:31

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.