title: Magento - Trying to create an edit admin Form, but it is giving blank input fields in the form
body: <p>I'm using Magento 1.9.0.1.</p>
  
  <p>I'm working on a custom Magento extension and so far i've created some custom pages in the admin panel. In one of them i've added a grid table which is fetching results from custom MySQL table.</p>
  
  <p>Let me show you a screen shot of this page:</p>
  
  <p><img src="https://i.stack.imgur.com/fAfNd.jpg" alt="enter image description here"></p>
  
  <p>Here is where the problem comes, when the Edit form is opened the input fields in it are empty. They must not be empty... as you know this is Edit form and there must be a data to be edited!</p>
  
  <p>Here is a screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/Pma3B.jpg" alt="enter image description here"></p>
  
  <p>As you can see the fields are empty... That's the problem...</p>
  
  <p>Let me show you all the code i think it is important for the resolving of the problem.</p>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/etc/config.xml:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;config&gt;
    &lt;modules&gt;
      &lt;VivasIndustries_SmsNotification&gt;
        &lt;version&gt;0.1.0&lt;/version&gt;
      &lt;/VivasIndustries_SmsNotification&gt;
    &lt;/modules&gt;
    &lt;global&gt;
      &lt;models&gt;
          &lt;smsnotification&gt;
              &lt;class&gt;VivasIndustries_SmsNotification_Model&lt;/class&gt;
              &lt;resourceModel&gt;vivasindustries_smsnotification_resource&lt;/resourceModel&gt;
          &lt;/smsnotification&gt;
          &lt;vivasindustries_smsnotification_resource&gt;
          &lt;class&gt;VivasIndustries_SmsNotification_Model_Resource&lt;/class&gt;
          &lt;entities&gt;
              &lt;smsnotification&gt;
              &lt;table&gt;VivasIndustries_SmsNotification&lt;/table&gt;
              &lt;/smsnotification&gt;
          &lt;/entities&gt;
          &lt;/vivasindustries_smsnotification_resource&gt;
      &lt;/models&gt;
      &lt;resources&gt;
          &lt;smsnotification_setup&gt;
              &lt;setup&gt;
                  &lt;module&gt;VivasIndustries_SmsNotification&lt;/module&gt;
              &lt;/setup&gt;
              &lt;connection&gt;
                   &lt;use&gt;core_setup&lt;/use&gt;
               &lt;/connection&gt;
          &lt;/smsnotification_setup&gt;
          &lt;smsnotification_read&gt;
              &lt;connection&gt;
                  &lt;use&gt;core_read&lt;/use&gt;
              &lt;/connection&gt;
          &lt;/smsnotification_read&gt;
          &lt;smsnotification_write&gt;
              &lt;connection&gt;
                  &lt;use&gt;core_write&lt;/use&gt;
              &lt;/connection&gt;
          &lt;/smsnotification_write&gt;
      &lt;/resources&gt;    
      &lt;events&gt;
          &lt;sales_order_save_after&gt;
              &lt;observers&gt;
                  &lt;vivasindustries_smsnotification&gt;
                      &lt;class&gt;smsnotification/observer&lt;/class&gt;
                      &lt;method&gt;orderSaved&lt;/method&gt;
                  &lt;/vivasindustries_smsnotification&gt;
              &lt;/observers&gt;
          &lt;/sales_order_save_after&gt;
      &lt;/events&gt;
      &lt;helpers&gt;
          &lt;smsnotification&gt;
              &lt;class&gt;VivasIndustries_SmsNotification_Helper&lt;/class&gt;
          &lt;/smsnotification&gt;
      &lt;/helpers&gt;
      &lt;blocks&gt;
          &lt;smsnotification&gt;
               &lt;class&gt;VivasIndustries_SmsNotification_Block&lt;/class&gt;
          &lt;/smsnotification&gt;
      &lt;/blocks&gt;
    &lt;/global&gt;
    &lt;adminhtml&gt;
      &lt;acl&gt;
          &lt;resources&gt;
              &lt;all&gt;
                  &lt;title&gt;Allow Everything&lt;/title&gt;
              &lt;/all&gt;
              &lt;admin&gt;
                  &lt;children&gt;
                      &lt;system&gt;
                          &lt;children&gt;
                              &lt;config&gt;
                                  &lt;children&gt;
                                      &lt;vivas&gt;
                                          &lt;title&gt;Vivas - All&lt;/title&gt;
                                      &lt;/vivas&gt;
                                  &lt;/children&gt;
                              &lt;/config&gt;
                          &lt;/children&gt;
                      &lt;/system&gt;
                  &lt;/children&gt;
              &lt;/admin&gt;
          &lt;/resources&gt;
      &lt;/acl&gt;
      &lt;layout&gt;
          &lt;updates&gt;
              &lt;smsnotification&gt;
                  &lt;file&gt;smsnotification.xml&lt;/file&gt;
              &lt;/smsnotification&gt;
          &lt;/updates&gt;
      &lt;/layout&gt;   
      &lt;/adminhtml&gt;
      &lt;admin&gt;
          &lt;routers&gt;
              &lt;adminhtml&gt;
                  &lt;args&gt;
                      &lt;modules&gt;
                          &lt;VivasIndustries_SmsNotification before="Mage_Adminhtml"&gt;VivasIndustries_SmsNotification_Adminhtml&lt;/VivasIndustries_SmsNotification&gt;
                      &lt;/modules&gt;
                  &lt;/args&gt;
              &lt;/adminhtml&gt;
          &lt;/routers&gt;
      &lt;/admin&gt;
  &lt;/config&gt;  
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/controllers/Adminhtml/SmsorderstatusesController.php:</p>
  
  <pre><code>&lt;?php
  
  class VivasIndustries_SmsNotification_Adminhtml_SmsorderstatusesController extends Mage_Adminhtml_Controller_Action
  {
      public function indexAction()
      {
          $this-&gt;_title($this-&gt;__('SMS Center'))-&gt;_title($this-&gt;__('SMS Center'));
          $this-&gt;loadLayout();
          $this-&gt;_setActiveMenu('vivassms');
          $this-&gt;_addContent($this-&gt;getLayout()-&gt;createBlock('smsnotification/adminhtml_sms_status'));
          $this-&gt;renderLayout();
      }
  
      public function gridAction()
      {
          $this-&gt;loadLayout();
          $this-&gt;getResponse()-&gt;setBody(
              $this-&gt;getLayout()-&gt;createBlock('smsnotification/adminhtml_sms_status_grid')-&gt;toHtml()
          );
      }
  
     public function newAction()
      {  
          $this-&gt;loadLayout();
          $this-&gt;_setActiveMenu('vivassms');
          $this-&gt;renderLayout();
      }  
  
  
  
      public function editAction()
      {
          /**
           * Retrieve existing brand data if an ID was specified.
           * If not, we will have an empty brand entity ready to be populated.
           */
          $brand = Mage::getModel('smsnotification/smsnotification');
  
  
  
          // process $_POST data if the form was submitted
          if ($postData = $this-&gt;getRequest()-&gt;getPost('smsData')) {
              try {
                  $brand-&gt;addData($postData);
                  $brand-&gt;save();
  
                  $this-&gt;_getSession()-&gt;addSuccess(
                      $this-&gt;__('The brand has been saved.')
                  );
  
                  // redirect to remove $_POST data from the request
                  return $this-&gt;_redirect(
                      'smsorderstatuses/edit',
                      array('id' =&gt; $brand-&gt;getId())
                  );
              } catch (Exception $e) {
                  Mage::logException($e);
                  $this-&gt;_getSession()-&gt;addError($e-&gt;getMessage());
              }
  
              /**
               * If we get to here, then something went wrong. Continue to
               * render the page as before, the difference this time being
               * that the submitted $_POST data is available.
               */
          }
  
          // Make the current brand object available to blocks.
          Mage::register('sms_brand', $brand);
  
          // Instantiate the form container.
          $brandEditBlock = $this-&gt;getLayout()-&gt;createBlock(
              'smsnotification/adminhtml_sms_status_edit'
          );
  
          // Add the form container as the only item on this page.
          $this-&gt;loadLayout()
              -&gt;_addContent($brandEditBlock)
              -&gt;renderLayout();
      }
  
      public function deleteAction()
      {
          $smsnotification = Mage::getModel('vivasindustries_smsnotification/smsnotification');
  
          if ($smsnotificationId = $this-&gt;getRequest()-&gt;getParam('id', false)) {
              $smsnotification-&gt;load($smsnotificationId);
          }
  
          if (!$smsnotification-&gt;getId()) {
              $this-&gt;_getSession()-&gt;addError(
                  $this-&gt;__('This smsnotification no longer exists.')
              );
              return $this-&gt;_redirect(
                  '*/*/index'
              );
          }
  
          try {
              $smsnotification-&gt;delete();
  
              $this-&gt;_getSession()-&gt;addSuccess(
                  $this-&gt;__('The smsnotification has been deleted.')
              );
          } catch (Exception $e) {
              Mage::logException($e);
              $this-&gt;_getSession()-&gt;addError($e-&gt;getMessage());
          }
  
          return $this-&gt;_redirect(
              '*/*/index'
          );
      }   
  
      protected function _initAction()
      {
          $this-&gt;loadLayout()
              // Make the active menu match the menu config nodes (without 'children' inbetween)
              -&gt;_setActiveMenu('vivassms')
              -&gt;_title($this-&gt;__('SMS Rule'))-&gt;_title($this-&gt;__('SMS Center'))
              -&gt;_addBreadcrumb($this-&gt;__('SMS Rule'), $this-&gt;__('SMS Rule'));
  
          return $this;
      }
  
  
      protected function _isAllowed()
      {
          return Mage::getSingleton('admin/session')-&gt;isAllowed('sales/foo_bar_baz');
      }
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Model/Smsnotification.php:</p>
  
  <pre><code>&lt;?php
  class VivasIndustries_SmsNotification_Model_Smsnotification extends Mage_Core_Model_Abstract
  {
      public function _construct()
      {
          parent::_construct();
          $this-&gt;_init('smsnotification/smsnotification');
      }
  
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification.php:</p>
  
  <pre><code>&lt;?php
  class VivasIndustries_SmsNotification_Model_Resource_Smsnotification extends Mage_Core_Model_Resource_Db_Abstract
  {
      /**
       * Initialize resource model
       *
       * @return void
       */
      public function _construct()
      {
          $this-&gt;_init('smsnotification/smsnotification','id');
      }
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification/Collection.php:</p>
  
  <pre><code>&lt;?php 
  class VivasIndustries_SmsNotification_Model_Resource_Smsnotification_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{
      protected function _construct(){
          $this-&gt;_init('smsnotification/smsnotification');    
      }
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status.php:</p>
  
  <pre><code>&lt;?php
  
  class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status extends Mage_Adminhtml_Block_Widget_Grid_Container
  {
      public function __construct()
      {
          $this-&gt;_blockGroup = 'smsnotification';
          $this-&gt;_controller = 'adminhtml_sms_status';
          $this-&gt;_headerText = Mage::helper('smsnotification')-&gt;__('Send SMS on Order Status Changes');
          $this-&gt;_addButtonLabel = Mage::helper('smsnotification')-&gt;__('Create new SMS Rule');
          parent::__construct();
      }
  
      protected function _prepareLayout()
      {
          $this-&gt;setChild( 'grid',
              $this-&gt;getLayout()-&gt;createBlock( $this-&gt;_blockGroup.'/' . $this-&gt;_controller . '_grid',
                  $this-&gt;_controller . '.grid')-&gt;setSaveParametersInSession(true) );
          return parent::_prepareLayout();
      }
  
  
  
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Grid.php:</p>
  
  <pre><code>&lt;?php
  
  class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Grid extends Mage_Adminhtml_Block_Widget_Grid
  {
      public function __construct()
      {
          parent::__construct();
          $this-&gt;setId('smsnotification_grid');
          $this-&gt;setDefaultSort('id');
          $this-&gt;setDefaultDir('DESC');
          $this-&gt;setSaveParametersInSession(true);
          $this-&gt;setUseAjax(true);
      }
  
  
      protected function _prepareCollection()
      {
          $collection = Mage::getResourceModel('smsnotification/smsnotification_collection');
          $this-&gt;setCollection($collection);
          return parent::_prepareCollection();
      }
  
  
      protected function _prepareColumns()
      {
            $this-&gt;addColumn('id', array(
                'header'    =&gt; Mage::helper('smsnotification')-&gt;__('ID'),
                'align'     =&gt;'right',
                'width'     =&gt; '50px',
                'index'     =&gt; 'id',
            ));
  
            $this-&gt;addColumn('Receiver', array(
                'header'    =&gt; Mage::helper('smsnotification')-&gt;__('Receiver'),
                'align'     =&gt;'left',
                'index'     =&gt; 'Receiver',
            ));
  
          $this-&gt;addColumn('Phone', array(
              'header'    =&gt; Mage::helper('smsnotification')-&gt;__('Phone'),
              'align'     =&gt;'left',
              'index'     =&gt; 'Phone',
          ));
  
          $this-&gt;addColumn('Date', array(
              'header'    =&gt; Mage::helper('smsnotification')-&gt;__('Date'),
              'align'     =&gt;'left',
              'index'     =&gt; 'Date',
  
          ));
  
  
          return parent::_prepareColumns();
      }
  
      public function getRowUrl($row)
      {
          return $this-&gt;getUrl('*/*/edit', array('id'=&gt;$row-&gt;getId()));
      }
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit.php:</p>
  
  <pre><code>&lt;?php
  class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
  {
      protected function _construct()
      {
          $this-&gt;_blockGroup = 'smsnotification';
          $this-&gt;_controller = 'adminhtml_sms_status';
  
  
          $this-&gt;_mode = 'edit';
  
          $newOrEdit = $this-&gt;getRequest()-&gt;getParam('id')
              ? $this-&gt;__('Edit')
              : $this-&gt;__('New');
          $this-&gt;_headerText =  $newOrEdit . ' ' . $this-&gt;__('SMS Rule');
      }
  }
  </code></pre>
  
  <p>Here is what i have in: /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit/Form.php:</p>
  
  <pre><code>&lt;?php
  class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
  {
      protected function _prepareForm()
      {
          // Instantiate a new form to display our brand for editing.
          $form = new Varien_Data_Form(array(
              'id' =&gt; 'edit_form',
              'action' =&gt; $this-&gt;getUrl(
                  'smsorderstatuses/edit',
                  array(
                      '_current' =&gt; true,
                      'continue' =&gt; 0,
                  )
              ),
              'method' =&gt; 'post',
          ));
          $form-&gt;setUseContainer(true);
          $this-&gt;setForm($form);
  
          // Define a new fieldset. We need only one for our simple entity.
          $fieldset = $form-&gt;addFieldset(
              'general',
              array(
                  'legend' =&gt; $this-&gt;__('Brand Details')
              )
          );
  
          $brandSingleton = Mage::getSingleton(
              'smsnotification/smsnotification'
          );
  
          // Add the fields that we want to be editable.
          $this-&gt;_addFieldsToFieldset($fieldset, array(
              'Receiver' =&gt; array(
                  'label' =&gt; $this-&gt;__('Receiver'),
                  'input' =&gt; 'text',
                  'required' =&gt; true,
              ),
              'Phone' =&gt; array(
                  'label' =&gt; $this-&gt;__('Phone'),
                  'input' =&gt; 'text',
                  'required' =&gt; true,
              ),
              'Date' =&gt; array(
                  'label' =&gt; $this-&gt;__('Date'),
                  'input' =&gt; 'text',
                  'required' =&gt; true,
              ),
          ));
  
          return $this;
      }
  
      protected function _addFieldsToFieldset(
          Varien_Data_Form_Element_Fieldset $fieldset, $fields)
      {
          $requestData = new Varien_Object($this-&gt;getRequest()
              -&gt;getPost('smsnotificationData'));
  
          foreach ($fields as $name =&gt; $_data) {
              if ($requestValue = $requestData-&gt;getData($name)) {
                  $_data['value'] = $requestValue;
              }
  
              // Wrap all fields with brandData group.
              $_data['name'] = "smsnotificationData[$name]";
  
              // Generally, label and title are always the same.
              $_data['title'] = $_data['label'];
  
              // If no new value exists, use the existing brand data.
              if (!array_key_exists('value', $_data)) {
                  $_data['value'] = $this-&gt;_getSms()-&gt;getData($name);
              }
  
              // Finally, call vanilla functionality to add field.
              $fieldset-&gt;addField($name, $_data['input'], $_data);
          }
  
          return $this;
      }
  
      protected function _getSms()
      {
          if (!$this-&gt;hasData('smsnotification')) {
              // This will have been set in the controller.
              $smsnotification = Mage::registry('sms_brand');
  
              // Just in case the controller does not register the brand.
              if (!$smsnotification instanceof
                      VivasIndustries_SmsNotification_Model_Smsnotification) {
                  $smsnotification = Mage::getModel(
                      'smsnotification/smsnotification'
                  );
              }
  
              $this-&gt;setData('smsnotification', $smsnotification);
          }
  
          return $this-&gt;getData('smsnotification');
      }
  }
  </code></pre>
  
  <p>So please tell me in which file i have an error so it is showing me empty fields.</p>
  
  <p>How can i fix this problem?</p>
  
  <p>Thanks in advance!</p>