2

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.

3

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.

  • Yes, I have this line of code. $form->setValues(Mage::registry('storeadvertisement_data')->getData()); – Charlie Aug 25 '14 at 13:18
  • Then it should work. Just remove 'value' => '', from the field definition. – Marius Aug 25 '14 at 13:19
  • Is there any difference between "addValues" and "setValues" functions – Charlie Aug 25 '14 at 13:20
  • Yes. setValues overrides any other values that were set previously. I suggest using addValues everywhere. Use setValues only in certain cases when you really know setValues is needed. – Marius Aug 25 '14 at 13:21
  • I change it addValues but no proper result – Charlie Aug 25 '14 at 13:24
3

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

0

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

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.