doutusheng5879 2012-03-05 19:10
浏览 29
已采纳

Zend_Form和Float数字本地化

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!

  • 写回答

2条回答 默认 最新

  • dtx3006 2012-03-06 04:39
    关注

    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')
        ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条