Search code examples
zend-frameworkzend-formzend-form-element

Render formatted numeric value in Zend_Form text field


I have a text field in a Zend_Form that contains a numeric value. I would like that value to be rendered as though it had been run through number_format(). For example, if the value in the field is 12345, I would like it to appear in the text field as 12,345.

On the submit side, I have a filter that strips the comma, giving me a clean numeric value when I eventually call $form->getValues(). [For the moment, let's forget about the locale issue]

But how do I format the value that ultimately appears in the input field?

I guess I could:

  1. create a custom element class extending Zend_Form_Element_Text
  2. create a custom view helper extending the FormText helper
  3. set the custom element to use the custom helper
  4. add the ViewHelperdecorator on the form element itself
  5. register all required paths/prefixes with all the forms and elements

But it sure seems like a whole lotta hoops through which to jump. Anything simpler?


Solution

  • Check out Zend_Locale_Format and set the value on your form element

    $element->setValue(Zend_Locale_Format::getNumber($value, array('number_format' => $xx, 'locale' => $lang_LOCALE));)