Magento: How to add checkbox field in Admin custom module form and populate the checkbox

How to add Checkbox field in Form:

Open app/code/local/Namespace/CustomModuleName/Block/Adminhtml/ CustomModuleName /Edit/Tab/Form.php and add the below code.


$fieldset->addField('checkbox_name', 'checkbox', array(        
          'name'      => ' checkbox_name ',        
          'onclick' => 'this.value = this.checked ? 1 : 0;',            
          'tabindex' => 1
        ));

How to re-populate the checkbox on Edit page:

Just add the extra line in ablove code like below:


$fieldset->addField('checkbox_name', 'checkbox', array(        
          'name'      => ' checkbox_name ',        
          'onclick' => 'this.value = this.checked ? 1 : 0;',            
          'tabindex' => 1
        ))->setIsChecked($model->getCheckboxName());


                                                                                                                                     Thanks, Done...
=========================================================================

Magento: How to add checkbox field in Admin custom module form and populate the checkbox Magento: How to add checkbox field in Admin custom module form and populate the checkbox Reviewed by Pranay Gondane on 3:34:00 AM Rating: 5

2 comments

  1. What is the $model variable? You haven't defined it in your code sample.

    ReplyDelete
    Replies
    1. Sorry for delay ...

      Please see the below example to understand the $model object.

      Example:

      $model = Mage::getModel('module/model_name')->load($edt_id);

      Here in $model object we are loading the data to edit the content with respect to the ID from DB.

      And $model->getCheckboxName();// getting the checkbox field value from db

      The 'CheckboxName' is the field name of checkbox which is going to return the checkbox field value (i.e true or flase).

      Do, let me know if you have further queries.

      Thanks,

      Delete

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!