Search code examples
magentomagento-1.7adminhtml

How to set default value in dropdown of admin grid form in magento?


I am working on Magento 1.7 version. My code is below:-

$categoryArray = Mage::getSingleton('lookbook/category')->getOptionArray();

    $catId = Mage::getSingleton('core/session')->getCatId();   //it has value 4

    $fieldset->addField('category_id', 'select', array(
        'label' => Mage::helper('lookbook')->__('Lookbook'),
        'name' => 'category_id[]',
        'values' => $categoryArray,            
        'value' => $catId,            
        'disabled' => true
    ));

How to set this value selected in drop-down of admin grid form in Magento?


Solution

  • Take a look @ https://magento.stackexchange.com/questions/544/how-to-set-default-value-for-form-fields

    protected function _prepareForm()
    {
        $form_data = new Varien_Object();
    
        $form = new Varien_Data_Form();
        $this->setForm($form);
        ....
    
        if ( Mage::getSingleton('adminhtml/session')->getXyzData() )
        {
            $form_data = Mage::getSingleton('adminhtml/session')->getXyzData();
            Mage::getSingleton('adminhtml/session')->setXyzData(null);
        } 
        else if ( Mage::registry('xyz_data') ) {
            $form_data = Mage::registry('xyz_data');  
        }
    
        $catId = Mage::getSingleton('core/session')->getCatId();   // it has value 4
    
        if( empty($form_data->getData('category_id')) ){
            $form_data->setData('category_id', $catId);
        }
    
        $fieldset->addField('category_id', 'select', array(
          'label' => Mage::helper('lookbook')->__('Lookbook'),
          'name' => 'category_id[]',
          'values' => $categoryArray,            
         'disabled' => true
        ));
    
    
       ....
    
        $form->setValues($form_data);
    
        $this->setForm($form);
    
    }