Set a value as selected in select dropdown custom admin form in Magento

I found this in Varien_Data_Form_Element_Select which generates the select option html

$fieldset->addField(
                'sample', 'select', array(
                    'label'              => 'Sample Dropdown',
                    'name'               => 'sample',
                    'class'              => '',
                    'note'               => '',
                    'style'              => '',
                    'values'=> array(1=>'Yes', 2=>'No')
                )
            );

Find: with in same form file

$form->setValues($values); 

Add line before:

// add this line just before setValues()
$values['sample'] = array(2);

if your form have no setValues() function just add value in addField like this

$fieldset->addField(
                'sample', 'select', array(
                    'label'              => 'Sample Dropdown',
                    'name'               => 'sample',
                    'class'              => '',
                    'note'               => '',
                    'style'              => '',
                    'values'=> array(1=>'Yes', 2=>'No')
                    'value'=> array(2)
                )
            );

option with value 2 will be selected.


Posted

in

by

Tags: