Magento – How to set the field values in a form

adminformmagento-1.9PHP

For Magento (1.9) how to set the value to the Magento form.

  $fieldset->addField('country_code', 'text', array(
          'label'     => Mage::helper('catalog')->__('Title3'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'country_code',
          'style'   => "border:10px",
          'value'  => 'hello !!',
          'disabled' => false,
          'readonly' => true,
        ));

I set the value is hello !! but it is not working.

enter image description here

Best Answer

Your problem is that values you set with addField() method are then overwritten with addValues() method which tries to set up a form fields values when form is used for editing existing entities or error occurred during submission.

Here is a workaround. Most likely you are getting values with a protected method like this:

protected function _getFormData()
{
    $data = Mage::getSingleton('adminhtml/session')->getFormData();

    if (!$data && Mage::registry('current_osmm_project')->getData()) {
        $data = Mage::registry('current_osmm_project')->getData();
    }

    return (array) $data;
}

So inside of your _prepareForm() method you replace:

$form->addValues($this->_getFormData());

with:

$_data = $this->__getFormData();
if (!empty($_data)) {
    $form->addValues($_data);
}
Related Topic