Model/Nic.php contains
public function toOptionArray()
{
$nicCollection = Mage::getModel('nic/nic')->getCollection();
$nicarray = array(array('value'=>-1, 'label'=>'Please Select'));
foreach ($nicCollection as $nicList){
$nicarray[] = array('value'=>$nicList['nic_id']._.$nicList['nic_cost'],'label'=>$nicList['nic_name']);
}
return $nicarray;
}
I have added a dropdown using toOptionArray
in admin form using below code
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$id = $this->getRequest()->getParam('id');
$nicModel = Mage::getModel('nic/nic')->load($id);
$fieldset = $form->addFieldset('nic_form', array('legend'=>Mage::helper('nic')->__('Nic Information')));
//Check if value selected from dropdown
$Id = Mage::registry('nic_data')->getId();
if($Id != ''){
$nicId = Mage::registry('nic_data')->getNicId();
$form->addValues(array('nic_id' => $nicId));
}
$fieldset->addField('nic_id', 'select', array(
'label' => Mage::helper('nic')->__('Nic'),
'class' => 'required-entry',
'values' => Mage::getModel('nic/nic')->toOptionArray(),
'name' => 'nic_id',
));
if ( Mage::getSingleton('adminhtml/session')->getCoffeecomponentsData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getCoffeecomponentsData());
Mage::getSingleton('adminhtml/session')->setCofeecomponentsData(null);
} elseif ( Mage::registry('nic_data') ) {
$form->setValues(Mage::registry('nic_data')->getData());
}
return parent::_prepareForm();
}
Which outputs
<select name="nic_id" id="nic_id">
<option value="-1">Please Select</option>
<option value="1_3.4">America</option>
<option value="2_2.3">China</option>
<option value="3_4.5">India</option>
<option value="4_2.5">Japan</option>
</select>
Here the value for China is 2
and its country cost value is 2.3
which has been split and then saved into DB as nic_id = 2
and nic_cost = 2.3
Now while editing the same record I am not able to select the value China in dropdown as the value fetched form Model is like 2_2.3
.
Also before add field I have used this code to make it selected, but somehow its not working
$Id = Mage::registry('nic_data')->getId();
if($Id != ''){
$nicId = Mage::registry('nic_data')->getNicId();
$form->addValues(array('nic_id' => $nicId ));
}
How can make it make it selected ?
Please Help !!!