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?

asked Apr 30, 2018 at 8:43

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]
 ]
 )
);
answered Apr 30, 2018 at 10:12
1
  • 1
    'value' => '1', 'values' => array( array('label' => 'Enabled', 'value' => '1'), array('label' => 'Disabled', 'value' => '0') ) working now! Commented 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)
 )
 )
 );
answered Mar 2, 2019 at 6:35
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. Commented Mar 2, 2019 at 6:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.