Magento 2 – Add Default Value for Admin Form Select Field

magento2

I am adding a select option with below code in my form

$fieldset->addField(
            'is_active',
            'select',
            [
                'label' => __('Status'),
                'title' => __('Status'),
                'name' => 'is_active',
                'required' => true,
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')]

            ]
        );

Now I need to add default value as 'Enabled'. Adding below line not helping.

'value' => '1'

I checked in vendor/magento/framework/Data/Form/Element/Select.php, below code ( line #57) is having empty value.

$value = $this->getValue();

Dont know if I am missing something. Can someone help?

Best Answer

Use this code. Hope this will help you.

$fieldset->addField(
    'is_active', 
    'select',
    array(
        'label' => __("Status"),
        'class' => 'required-entry',
        'required' => 'true',
        'name' => 'is_active',        
        'value' => 1,
        'values'    => [
            ['label' => 'Enabled', 'value' => 1],
            ['label' => 'Disabled', 'value' => 0]
        ]
    )
);
Related Topic