Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Using Checkboxes in Magento's Backend
Alexander Turiak Avatar

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:

$form->getElement('is_enabled')->setIsChecked(!empty($formData['is_enabled']));

 

Lastly – saving the value on form submission in your controller:

$model->setIsEnabled(!empty($postData['is_enabled']));

 

I hope this article helps you to get checkboxes working in your module. Please leave a comment if it does. Thanks for reading!

 

Read more: