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?

3
  • 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
    Commented Feb 6, 2013 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. Commented Feb 6, 2013 at 12:49
  • 1
    @Peter- Yess! It was the issue...! Thanks, I was using setValues..
    – Vishnu R
    Commented Feb 6, 2013 at 13:54

6 Answers 6

7

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

2
  • yes. it is displayed. :(
    – Vishnu R
    Commented Feb 6, 2013 at 10:51
  • 5
    @Anna Völkl: you are right but if addValues() method is used afterwards those values are being rewritten. Commented Feb 8, 2013 at 10:46
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);
}
0
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.

1
  • please see the edit to my answer. Commented Feb 8, 2013 at 10:44
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.

0

You can use

$data['ajax_time_interval'] = 120;

where $data is an array of form fields.

$data['{field_name}'] = '{field_name}';
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', 
  ));
1
  • It's Not working
    – Hemant
    Commented Feb 24, 2019 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.