You want to manipulate the value of a standard text field. One of the most common solutions for doing this is to add a custom form element to your adminhtml form. This can be done by creating your own element class and extending the Varien_Data_Form_Element_Abstract. This method will work, but seems a bit much just to manipulate the value.
I dug around a bit and found a perfect solution. Varien_Data_Form_Abstract provides a nifty method “addType“. So just before we add our new form field, we would need to add the new element type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
.... /** * Add new element type 'price' * param1: 'price' is the new type * param2: 'Fontera_Trader_Block_Adminhtml_Games_Edit_Tab_Renderer_Price' is the class that will be rendered */ $fieldset->addType('price', 'Fontera_Trader_Block_Adminhtml_Games_Edit_Tab_Renderer_Price'); /** * Add field where type is 'price' */ $fieldset->addField('buy_in_amount', 'price', array( 'label' => $helper->__('Buy In Amount'), 'title' => $helper->__('Buy In Amount'), 'name' => 'buy_in_amount', 'class' => 'required-entry', 'required' => true, )); .... |
The class “Fontera_Trader_Block_Adminhtml_Games_Edit_Tab_Renderer_Price” will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Fontera_Trader_Block_Adminhtml_Games_Edit_Tab_Renderer_Price extends Varien_Data_Form_Element_Text { /** * Get escaped value * * format value to 2 decimal places */ public function getEscapedValue($index=null) { $value = $this->getValue($index); // format to 2 decimal places $value = number_format($value, 2); if ($filter = $this->getValueFilter()) { $value = $filter->filter($value); } return $this->_escape($value); } } |
The class “Fontera_Trader_Block_Adminhtml_Games_Edit_Tab_Renderer_Price” can extend any one of the defined element types in “lib/Varien/Data/Form/Element/” or you could just extend “Varien_Data_Form_Element_Abstract” and create your own element type.
Thats it…