Question

I have a problem while editing my custom admin module.

Custom module working fine but while editing I have to a display select box value as selected from database records,

I have two values and in database the datatype is enumeration. the values are

1=> image 2=> video

While editing I have to display image or video based on the database value. but I am not getting proper result here is my code.

$fieldset->addField('add_type', 'select', array(
          'label'     => Mage::helper('advertisement')->__('Media'),
          'name'      => 'add_type',
          'onchange' => 'checkSelectedItem(this.value)',
          'value'  => '',
          'values' => array(array('value'=>'0','label'=>'Select Type'),
                            array('value'=>'1','label'=>'Image'),
                            array('value'=>'2','label'=>'Video'),
                       ),
);

I have Tried this too

$recordId = Mage::registry('storeadvertisement_data')->getData();
        $selectVal = Mage::getModel('advertisement/advertisement')->load($recordId['id'])->getData('add_type');
  $fieldset->addField('add_type', 'select', array(
          'label'     => Mage::helper('advertisement')->__('Media'),
          'name'      => 'add_type',
          'onchange'  => 'checkSelectedItem(this.value)',
          'value'     => $selectVal,
          'values'    => array(array('value'=>'0','label'=>'Select Type'),
                            array('value'=>'1','label'=>'Image'),
                            array('value'=>'2','label'=>'Video'),
                       ),
);

But it did not help.

Was it helpful?

Solution

At the end of your form before calling return parent::_prepareForm();:

$form->addValues(Mage::registry('storeadvertisement_data')->getData());

If your form variable is not named $form, change the variable name also in the code above.

OTHER TIPS

I got answer. I did mistake while getting the value from database and adding the name of the field name options, Actualy the I am getting values from database as follows

Array
        (
            [id] => 29
            [store_id] => 8
            [add_type] => video
            [add_name] => beckmanpro 2
            [description] => seccond add for beckmanpro 
            [status] => active
            [url] => //www.youtube.com/embed/xVXZzm_bjHE
            [link] => http://canon.com
            [created_at] => 2014-08-25 11:14:09
            [updated_at] => 2014-08-25 11:14:09
        )

check the value of add_type in above array. and my previous code as follows.

$fieldset->addField('add_type', 'select', array(
        'label' => Mage::helper('advertisement')->__('Media'),
        'title' => Mage::helper('advertisement')->__('Media'),
        'name' => 'add_type',
        'required' => true,
        'options' => array(
            '0' => 'Select Type',
            '1' => 'Image',
            '2' => 'Video',
        ),
    ));

Check options in above code. the mistake is done here,

The array say's that the value for add_type comes as video, but I am adding the value as 0, 1, 2 for each, so I change my code as follows

$fieldset->addField('add_type', 'select', array(
          'label'     => Mage::helper('advertisement')->__('Media'),
          'name'      => 'add_type',
          'options' => array(
            '0' => 'Select Type',
            'image' => 'Image',
            'video' => 'Video',
        )
);

Thank you all. and thank you @Marius and @R.S

Try

  $fieldset->addField('add_type', 'select', array(
        'label' => Mage::helper('advertisement')->__('Media'),
        'title' => Mage::helper('advertisement')->__('Media'),
        'name' => 'add_type',
        'required' => true,
        'options' => array(
            '0' => 'Select Type',
            '1' => 'Image',
            '2' => 'Video',
        ),
        'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
    ));

Then

 if ( Mage::getSingleton('adminhtml/session')->get<Module>Data() ){
      $form->setValues(Mage::getSingleton('adminhtml/session')->get<Module>Data());
      Mage::getSingleton('adminhtml/session')->set<Module>Data(null);
 } elseif ( Mage::registry('<module>_data') ) {
     $form->setValues(Mage::registry('<module>_data')->getData());
 }

Try clearing cache

see Magento - Wiki - Custom Module with Custom Database Table

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top