I have created a form by exending Magento\Backend\Block\Widget\Form\Generic class in my class.
I am adding a field using $fieldset->addField() to my form for which I need to add "Use Default Value" checkbox field like below screenshot.
With dataProvider class in ui_component form, this is done by setting
'arguments' => [
'data' => [
'config' => [
'dataType' => 'text',
'formElement' => 'input',
'componentType' => 'field',
'label' => 'Field',
'validation' => [
'validate-digits' => true,
],
/*
* Below line of code adds the checkbox which
* Enable/Disable input field when checked
*/
'service' => [
'template' => 'ui/form/element/helper/service',
],
],
],
]
Any idea on how to set it for my addField function? I have tried it adding as a config parameter but its not working.
$fieldset->addField(
'field',
'text',
[
'name' => 'field',
'label' => __('Field'),
'title' => __('Field'),
'required' => true,
'service' => [
'template' => 'ui/form/element/helper/service',
]
]
);
asked Jan 29, 2021 at 7:43
Jaimin Sutariya
11.2k5 gold badges38 silver badges72 bronze badges
-
where is this field displayed?Shawn Abramson– Shawn Abramson2021年01月29日 08:17:45 +00:00Commented Jan 29, 2021 at 8:17
-
@ShawnAbramson, Its in my admin panel custom form.Jaimin Sutariya– Jaimin Sutariya2021年01月29日 08:37:20 +00:00Commented Jan 29, 2021 at 8:37
-
please share full code.Himanshu– Himanshu2021年01月29日 09:34:28 +00:00Commented Jan 29, 2021 at 9:34
-
@Himanshu, I added the complete code for ui_component form field. Its working with ui_component form but not with generic formsJaimin Sutariya– Jaimin Sutariya2021年01月29日 09:46:05 +00:00Commented Jan 29, 2021 at 9:46
1 Answer 1
Try
$fieldset->addField(
'field',
'checkboxes',
[
'name' => 'field',
'label' => __('Field'),
'title' => __('Field'),
'required' => true,
'values' => [['value'=>'1', 'label'=> 'Use Default Value']],
'checked' =>array(1)
]
);
-
Thank you Himanshu for the answer. If you have ever seen "Use default value" checkbox in Magento admin, it is connected to another field. When the checkbox is checked, the field uses default value and is disabled. When we uncheck the checkbox, the field gets enabled and you can change its value. Its not a separate checkbox field I want to add.Jaimin Sutariya– Jaimin Sutariya2021年01月29日 10:20:15 +00:00Commented Jan 29, 2021 at 10:20
default