Magento2 Admin Form – Add Checkbox in Magento 2 Admin Form

adminadminformmagento2

I have a custom module, it has two fields, table_name and table_number and I have added new field like is_reserve to check if table is reserved,

I have added column in table with integer data type, now I need to add a checkbox for this field on admin form:

$fieldset->addField(
                 'is_reserved',
                 'checkbox',
                 array(
                     'name' => 'is_reserved',
                     'label' => __('is reserved'),
                     'title' => __('is reserved'),
                     'onclick'   => 'this.value = this.checked ? 1 : 0;',
                     /*'required' => true,*/
                 )
             );

I have added this in block file, what else I need to do to get my checkbox working? It will be a great help if someone can give any hint about this.

Thanks

Best Answer

Take a look: vendor/magento/module-customer/Block/Adminhtml/Edit/Tab/Newsletter.php:

$fieldset->addField(
            'subscription',
            'checkbox',
            [
                'label' => __('Subscribed to Newsletter'),
                'name' => 'subscription',
                'data-form-part' => $this->getData('target_form'),
                'onchange' => 'this.value = this.checked;'
            ]
        );

We can follow the same logic.