1

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.

enter image description here

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
4
  • where is this field displayed? Commented Jan 29, 2021 at 8:17
  • @ShawnAbramson, Its in my admin panel custom form. Commented Jan 29, 2021 at 8:37
  • please share full code. Commented 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 forms Commented Jan 29, 2021 at 9:46

1 Answer 1

0

Try

$fieldset->addField(
 'field',
 'checkboxes',
 [
 'name' => 'field',
 'label' => __('Field'),
 'title' => __('Field'),
 'required' => true,
 'values' => [['value'=>'1', 'label'=> 'Use Default Value']],
 'checked' =>array(1)
 ]
);
answered Jan 29, 2021 at 10:00
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. Commented Jan 29, 2021 at 10:20

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.