0

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',
        ]
    ]
);
4
  • where is this field displayed? – Shawn Abramson Jan 29 at 8:17
  • @ShawnAbramson, Its in my admin panel custom form. – Jaimin Sutariya Jan 29 at 8:37
  • please share full code. – Himanshu Jan 29 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 – Jaimin Sutariya Jan 29 at 9:46
0

Try

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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