Most commonly, a dropdown box with Yes / No options is used in Magento’s backend to designate an option with a boolean value. This is most likely done due to the complexity of using checkboxes; however it is still possible.
To add a checkbox field to a form, the following code should be used:
$fieldSet->addField('is_enabled', 'checkbox', array( 'label' => Mage::helper('module')->__('Enable feature?'), 'onclick' => 'this.value = this.checked ? 1 : 0;', 'name' => 'is_enabled', ));
Note the line “
'onclick' => 'this.value = this.checked ? 1 : 0;'“, which sets the value attribute of
input type="checkbox" element to the required value depending on whether the checkbox is checked or cleared.
Next we need to populate the checkbox with data from our model. This is done with the following snippet:
Lastly – saving the value on form submission in your controller:
I hope this article helps you to get checkboxes working in your module. Please leave a comment if it does. Thanks for reading!