Magento 1 – Get Value of Select Dropdown in Edit Form Fieldset

adminformadminhtmlformsmagento-1

I am working on an edit screen for my grid row. This is what I have so far for this form:

<?php

class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        $fieldset = $form->addFieldset('integration_form', array(
            'legend' => Mage::helper('integration')->__('Asendia Pick Information')
        ));

        $fieldset->addField('order_number', 'label', array(
            'label' => Mage::helper('integration')->__('Order Number'),
            'name' => 'order_number'
        ));

        // snipped

        $fieldset->addField('pick_status', 'select', array(
            'required' => false,
            'class' => 'required-entry',
            'label' => Mage::helper('integration')->__('Pick Status'),
            'name' => 'pick_status',
            'values' => Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray(),
            'readonly' => 'readonly'
        ));

        // snipped

        return parent::_prepareForm();
    }
}

This produces the following output in the admin backend:

enter image description here

What I would like to do is change the pick_status column from a select to a label. When I do this, instead of showing the status value "New" it shows the array index like this:

enter image description here

My option array for asendiapickstatus is defined like this in my model:

class Intellibi_Integration_Model_Asendiapickstatus extends Varien_Object
{
    const PICK_STATUS_NEW       = 1;
    const PICK_STATUS_SENT      = 2;
    const PICK_STATUS_SHIPPED   = 3;

    static public function getOptionArray()
    {
        return array(
            self::PICK_STATUS_NEW       => Mage::helper('integration')->__('New'),
            self::PICK_STATUS_SENT      => Mage::helper('integration')->__('Sent'),
            self::PICK_STATUS_SHIPPED   => Mage::helper('integration')->__('Shipped')
        );
    }
}

So my question is; on the edit form fieldset builder, how do I show the dropdown field "pick_status" value, rather than the current index it's at? So the output will say "New" instead of "1" as shown above. Will I need a custom renderer?

Best Answer

I've solved it like this (with a custom form rendered element):

Added custom fieldset type

$fieldset->addType('pickstatus', 'Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus');

Used the fieldset like this

$fieldset->addField('pick_status', 'pickstatus', array(
    'label' => Mage::helper('integration')->__('Pick Status'),
    'name' => 'pick_status',
));

Coded the rendered like this

class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus extends Varien_Data_Form_Element_Abstract
{
    protected $_element;

    public function getElementHtml()
    {
        // Load Pick Status
        $pick_status = (int)$this->getValue();
        $pick_status_list = Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray();

        // Finish
        return array_key_exists($pick_status, $pick_status_list) ? $pick_status_list[$pick_status] : 'Unknown';
    }
}

And it renders like this

enter image description here