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开发者_如何学Go field?
I guess I could:
- create a custom element class extending
Zend_Form_Element_Text
- create a custom view helper extending the
FormText
helper - set the custom element to use the custom helper
- add the
ViewHelper
decorator on the form element itself - 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?
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));)
精彩评论