1

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?

2 Answers 2

3

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]
        ]
    )
);
1
  • 1
    'value' => '1', 'values' => array( array('label' => 'Enabled', 'value' => '1'), array('label' => 'Disabled', 'value' => '0') ) working now! Apr 30, 2018 at 10:43
0

Ronak Parmars code is correct however syntax problems. I dont have the Rep to comment so I have posted the correct copy.

    $fieldset->addField(
             'is_active', 
             'select',
              array(
                   'label' => __("Status"),
                   'class' => 'required-entry',
                   'required' => 'true',
                   'name' => 'is_active',        
                   'value' => 1,
                   'values'    => array(
                         array('label' => 'Enabled', 'value' => 1),
                         array('label' => 'Disabled', 'value' => 0)
                         )
                   )
             );
1
  • Welcome to MSE. As you are already know that this section is for posting answer only so I encourage to follow same for next posts.
    – Jarnail S
    Mar 2, 2019 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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