19

I have an admin form field(textbox),

$fieldset->addField('ajax_time_interval', 'text', array(
          'label'     => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'ajax_time_interval',
      ));

I need to set a default value for this text field. I tried, setting 'value' => '120', in it. But its not working.

$fieldset->addField('ajax_time_interval', 'text', array(
          'label'     => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'ajax_time_interval',
          'value'     => '120', 
      ));

How to set a defalt value in that field?

Vishnu R
  • 597
  • 1
  • 6
  • 21
  • 1
    Check that it's not a browser cache issue. I has this serveral times, especially in Firefox, which is caching the latest values. – Tobias Feb 06 '13 at 11:12
  • 5
    Have you verified you are not doing setValues on the form after field definition? If you do and object used (or array) is empty, then all values are cleared. – Petar Dzhambazov Feb 06 '13 at 12:49
  • 1
    @Peter- Yess! It was the issue...! Thanks, I was using setValues.. – Vishnu R Feb 06 '13 at 13:54

6 Answers6

12

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);
}
Tim Bezhashvyly
  • 11,575
  • 6
  • 43
  • 73
7

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

Anna Völkl
  • 17,341
  • 5
  • 60
  • 141
2

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.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Vishnu R
  • 597
  • 1
  • 6
  • 21
1

For anyone looking and are working on 1.9 and have the below

if($model){
    $form->setValues($model->getData());
}

You can do the following without having to edit your existing data, just above that code place:

if($model->getData('fieldName') == ""){
    $model->setData('fieldName', "yourValue");
}

Of course you can check isset and such but here is the basic version of setting defaults.

MageIdiot
  • 138
  • 6
0

=> You can add 'default' attribute in form fields. Like this :

        $fieldset->addField('ajax_time_interval', 'text', array(
      'label'     => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'ajax_time_interval',
      'default'     => '120', 
  ));
Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96
apandey846
  • 113
  • 7
0

You can use

$data['ajax_time_interval'] = 120;

where $data is an array of form fields.

$data['{field_name}'] = '{field_name}';
Marius
  • 197,939
  • 53
  • 422
  • 830
Anshu Mishra
  • 8,950
  • 7
  • 40
  • 88