1

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 !!!

2
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);

}

0
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')));

      $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();
  }
  • I have the same code. What is the change ? – Mark Jun 10 '14 at 12:03
  • can you post whole code of the form please. – Pradeep Sanku Jun 10 '14 at 12:06
  • Check the edited code now – Mark Jun 10 '14 at 12:16
  • now use the edited code.i had removed the form->add value code then the above code is working for me.you can try it. – Pradeep Sanku Jun 10 '14 at 12:26
  • setValue will do the work for you. – Pradeep Sanku Jun 10 '14 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.