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
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'value' => '1', 'values' => array( array('label' => 'Enabled', 'value' => '1'), array('label' => 'Disabled', 'value' => '0') ) working now!Pallavi Sinha– Pallavi Sinha2018年04月30日 10:43:25 +00:00Commented Apr 30, 2018 at 10:43
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)
)
)
);
-
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– Jarnail S2019年03月02日 06:44:25 +00:00Commented Mar 2, 2019 at 6:44