Michal Borek
Michal Borek

Reputation: 4624

Zend_Form and Float number localization

I'm using Zend_Form for handling a form and I have a problem with localization.

I use following field declaration:

$this->addElement('text', 'area', array(
        'label' => 'Area:',
        'required' => true,
        'filters' => array('StringTrim', 'NormalizedToLocalized')
    ));

The problem is, I use pl_PL locale and in this locale the decimal point separator is "," not ".". But database (MySQL) stores float with "." separator.

I added NormalizedToLocalized to convert e.g. 40.12 into 40,12 in my html form.

But when passing 40,12 in POST request I want Zend_Form to automatically convert back value 40,12 into 40.12 so that it can be passed to DB (I collect values from form using $form->getValues() method). By now it renders 40,12 successfully but in $form->getValues() I get localized, not not normalized value.

So my question is, whether it is possible to create different filter for rendering and getting value of field in Zend Framework.

Thanks in advance for your help!

Upvotes: 0

Views: 2389

Answers (2)

RockyFord
RockyFord

Reputation: 8519

The filters are not applied until the form is submitted so 'NormalizedToLocalized' will not have any effect until the form is posted. So if you want decimal points and not commas in your data for storage remove the filter (or use 'LocalizedToNormalized') from the form and apply the filter using Zend_Filter for any data you need to display.
If this field always expects a float type you can use a validator to enforce the value and then use the filter to enforce formating for the database.

$this->addElement('text', 'area', array(
        'label' => 'Area:',
        'required' => true,
        'validators' => array('Float'),
        'filters' => array('StringTrim', 'LocalizedToNormalized')
    ));

Upvotes: 2

dev-null-dweller
dev-null-dweller

Reputation: 29482

I'm not familiar with Zend Framework but looking on filter name, shouldn't it be 'LocalizedToNormalized' ?

Upvotes: 0

Related Questions