Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How to set a default value in a form select field? 
 
Emperor
Sr. Member
 
Total Posts:  111
Joined:  2007-08-31
 

I have the following filed and I want to select a default value for the user to see:

$fieldset->addField('ids''select', array(
                    
'name'  => 'ids',
                    
'label' => Mage::helper('adminhtml')->__('Id Numbers'),
                    
'title' => Mage::helper('adminhtml')->__('Id Numbers'),
                    
'required' => true,
                    
'values' => $options,
                )
            );
 
Magento Community Magento Community
Magento Community
Magento Community
 
frano9
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2010-01-21
Manchester
 

You probably don’t need this anymore but for people who are still searching for it :

You just need to add “value” and define the selected value there.

For eg:

$fieldset->addField('ids''select', array(
                    
'name'  => 'ids',
                    
'label' => Mage::helper('adminhtml')->__('Id Numbers'),
                    
'title' => Mage::helper('adminhtml')->__('Id Numbers'),
                    
'required' => true,
                    
'values' => $options,
                    
'value' => 'default',
                )
            );
 
Magento Community Magento Community
Magento Community
Magento Community
 
rajaehtesham
Jr. Member
 
Total Posts:  4
Joined:  2011-02-20
 

$fieldset->addField('status''select', array(
            
'label'     => Mage::helper('mymodule')->__('Status'),
            
'name'      => 'status',
            
'values'    => array(

                array(
                    
'value'     => 0,
                    
'label'     => Mage::helper('mymodule')->__('Inactive'),
                ),
                
                array(
                    
'value'     => 1,
                    
'label'     => Mage::helper('mymodule')->__('Active'),
                ),                
            ),
            
'value' => 1,
        ));

the above code is not working, i am still getting ‘Inactive’ as selected by default. any suggestions ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
frano9
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2010-01-21
Manchester
 

It looks like you have a double array instead of a simple array.

It should be like that :

$options = array(
                
=> $this->__('Yes'),
                
=> $this->__('No'),
            );

then :

...
'values' => $options,
'value' => 1,
....
 
Magento Community Magento Community
Magento Community
Magento Community
 
rajaehtesham
Jr. Member
 
Total Posts:  4
Joined:  2011-02-20
 

Thanks, I tried this but still “No” is selected as default.  I am using version 1.5 if that matters. I dont know what I am doing wrong here is my code :

$options = array(                
                
=> $this->__('No'),
                
=> $this->__('Yes'),
            );

        
$fieldset->addField('status''select', array(
            
'label'     => Mage::helper('myproductfamily')->__('Status'),
            
'name'      => 'status',
            
'values'    => $options,
            
'value' => 1,
        ));
 
Magento Community Magento Community
Magento Community
Magento Community
 
abeauchemin
Member
 
Total Posts:  38
Joined:  2011-09-22
 

I confirm, it doesn’t work.

Any solution yet?

 
Magento Community Magento Community
Magento Community
Magento Community
 
alpha1125
Jr. Member
 
Total Posts:  3
Joined:  2010-08-04
Toronto, Canada
 

The above does not work…

This does…

class Company_Web_Block_Adminhtml_Web_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    
protected function _prepareForm()
    
{

// ... setup your fields... / colums... 
// $fieldset->addField('field', .........


// set defaults, to match your fieldname.
        
$data['is_active'1;
        
$data['order'1;
        
$form->setValues($data);

        return 
parent::_prepareForm();
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
satish
Enthusiast
 
Total Posts:  812
Joined:  2008-03-24
34,mantri Sadan,Shivaji Nagar,Nagpur,MS,India 4400
 

You will set the select field value in model after EditAction.
Sample code:
$model[\’select_value\’] = \’Your Values are set here\’;

 
Magento Community Magento Community
Magento Community
Magento Community
 
k0nqu3r0r
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2012-03-25
 

Hi all,

I’ve made it this way:

$fieldset->addField('status''select', array(
            
'label'     => Mage::helper('carousel')->__('Status'),
            
'name'      => 'status',
            
'values'    => array(
                array(
                    
'value'     => '1'//You should wrap values in brackets - "value"
                    
'label'     => Mage::helper('carousel')->__('Active'),
                ),
 
                array(
                    
'value'     => '0'//You should wrap values in brackets - "value"
                    
'label'     => Mage::helper('carousel')->__('Inactive'),
                ),
            ),
            
'value'     => //Default value
        
));

That solved my problem.

Also look in Database for your presets - many modules have 0 as default in Status column.

You can make sql upgrade script:

<?php 

$installer 
$this;
/* @var installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$installer->run("
ALTER TABLE {
$installer->getTable('your_table')} 
    MODIFY COLUMN status smallint(6) NOT NULL default '1';
"
);

$installer->endSetup();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top