How to set default value for form fields?

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);
}

Is the textfield itself displayed? The setting 'value' => '120' seems correct to me.


Thanks everybody for spending your valuable time and effort. I was Using setValues() after addfields. So the default values were being cleared.

I overcome it by providing an if loop, such as:

if (Mage::registry('dealroom_data')->getAjaxTimeInterval() < 0 || Mage::registry('dealroom_data')->getAjaxTimeInterval() == "") {
    Mage::registry('dealroom_data')->setAjaxTimeInterval(120);
}

120 is my default time here.

Tags:

Magento 1