Magento Admin Edit Form Fields - Custom fields for model (s)

Wanting to add a custom model for rendering in magento custom form. I just can't get the original model to display any of the parameters. It should be a simple task to do this, but maybe I am missing something. Couldn't find anything on google as it is mostly related to examples of original system / configuration models. See code below

Model File (My / Module / Model / MyModel.php)

<?php
class My_Module_Model_MyModel extends Mage_Core_Model_Abstract
{
 static public function getOptionArray()
{

    $allow = array(
       array('value' => '1', 'label' => 'Enable'),
       array('value' => '0', 'label' => 'Disable'),
   );

   return $allow;
}
}

      

and my form tabs file - the tab is shown with a multi selector field but its empty (My / Module / Block / Adminhtml / Module / Edit / Tab / Data.php)

<?php

class My_Module_Block_Adminhtml_Module_Edit_Tab_Data extends Mage_Adminhtml_Block_Widget_Form
{

protected function _prepareForm(){ 


    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('module_form', array('legend'=>Mage::helper('module')->__('Module Information')));
    $object = Mage::getModel('module/module')->load( $this->getRequest()->getParam('module_id') );


    echo $object;



    $fieldset->addField('module_enabled', 'multiselect', array(
      'label'     => Mage::helper('module')->__('Allowed Module'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'module_enabled',
      'source_model' => 'My_Module_Model_MyModel',
      'after_element_html' => '<small>Select Enable to Allow</small>',
      'tabindex' => 1
    ));


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


  }

      

So I have other fields, tabs that save data, etc., but just can't get the values ​​to render using the custom model inside the multi selector field.

Any help would be awesome!

greetings

========== EDIT ===========

updated MyModel.php to get foreach on collection (like CMS pages)

<?php
class My_Module_Model_MyModel
{
 public function toOptionArray($withEmpty = false)
 {
    $options = array();
    $cms_pages = Mage::getModel('cms/page')->getCollection();
    foreach ($cms_pages as $value) {
        $data = $value->getData();
        $options[] = array(
            'label' => ''.$data['title'].'('.$data['identifier'].')',
            'value' => ''.$data['identifier'].''
        );
    }

    if ($withEmpty) {
        array_unshift($options, array('value'=>'', 'label'=>Mage::helper('module')->__('-- Please Select --')));
    }

    return $options;
}

      

and inside My / Module / Block / Adminhtml / Module / Edit / Tab / Data.php I just removed the "source_model" and replaced it with

'values' => Mage::getModel('module/mymodel')->toOptionArray(),

      

============= Other Editing ====================

Just to add, there was also a problem with multi-element values ​​not preserving / updating the multisex field when updating / saving in the edit page. To get this working, I edited the admin controller named saveAction (or the name of the action to save the form data). See below my saveAction in controller for admin / backend located at My / Module / controller / Adminhtml / ModuleController.php

 public function saveAction() {
    $model = Mage::getModel('module/module');
    if ($data = $this->getRequest()->getPost()) {


        $model = Mage::getModel('module/module');
        $model->setData($data)
            ->setModuleId($this->getRequest()->getParam('module_id'));

        try {
            if ($model->getCreatedTime() == NULL || $model->getUpdateTime() == NULL) {
                $model->setCreatedTime(now())->setUpdateTime(now());
            } else {
                $model->setUpdateTime(now());
            }


            $ModuleEnabled = $this->getRequest()->getParam('module_enabled');
            if (is_array($ModuleEnabled))
            {
            $ModuleEnabledSave = implode(',',$this->getRequest()->getParam('module_enabled')); 
            }
            $model->setModuleEnabled($ModuleEnabledSave);

            //save form data/values per field
            $model->save();


            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('module')->__('Item was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);

            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('module_id' => $model->getModuleId()));
                return;
            }
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('module_id' => $this->getRequest()->getParam('module_id')));
            return;
        }
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('module')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}

      

This saves the popup array (i.e. 2, 3, 6, 23, 28,) to the database value and displays the selected fields of the multi-selections on the appropriate tab when updating / updating / saving

Hope this helps anyone and thanks Reindex for your quick response

+3


source to share


1 answer


It looks like the name of the method in the original model is wrong. Also, you probably don't need to expand Mage_Core_Model_Abstract

in the original models.

Try the following:



<?php
class My_Module_Model_MyModel
{
    public function toOptionArray()
    {
        return array(
            array('value' => '1', 'label' => Mage::helper('module')->__('Enable')),
            array('value' => '0', 'label' => Mage::helper('module')->__('Disable')),
        );
    }
}

      

+3


source







All Articles