dsdeeaquu38538545 2014-05-14 22:34 采纳率: 100%
浏览 40
已采纳

ZF2验证日期和时间格式PT_BR始终输入似乎不是有效日期

Project utilizing ZF2 + Doctrine 2.

I Tried many formats. I'm working with a Form without validation.

My last try was:

    $traindate = new Element\DateTime('trainDate');
    $traindate->setAttributes(array(
        'name' => 'trainDate',
        'id' => 'trainDate',
        'size' => '30',
        'class' => 'datepicker',
        'options' => array(
            'label' => '',
            'format' => 'd-m-Y H:i'
        ),
    ));

I need to use a input to set a date and time of a event. On Brazil the basic format is: 14-05-2014 14:20 15-05-2015 15:00

With means Days Months Year Hour Minutes, like I'm expressing on the Options -> Format.

This way always when I try to insert, i get the following messsage: The input does not appear to be a valid date

Removing the format, i can only pass by $form->isValid($data) by Y-m-d (American Format), but by the way i can't pass time to date too, which is causing me big troubles.

I need to set date PT_BR on Form/Input, Pass by validation, Convert Data do Mysql Format (YYYY-mm-dd HH:ii:ss). And then retrive from db and convert back to pt_br format.

But not even i can pass time with date to zf2 form, ever this error message.

I remove all filters from this form trying to get work, but doesn't work.

Where is the main problem?

  • 写回答

2条回答 默认 最新

  • dreamy1992 2014-11-26 15:48
    关注

    After a long time paying my attention to this problem I found the right and quick solution. After 6 month making science, I got:


    Right all:

        $traindate = new Element\DateTime('trainDate');
        $traindate->setAttributes(array(
            'name' => 'trainDate',
            'id' => 'trainDate',
            'size' => '30',
            'class' => 'datepicker',
        ));
        $traindate->setFormat('d/m/Y'); //ONLY WORKS ON THIS FORMAT.
    

    Docs and people over internet don't make it clear, but to set Format only works on this form.

    And to grab this to Entity, you need to write your own Hydrator extending the DoctrineHydrator:

    namespace Application\Hydrator;
    use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
    
    class MyCustomHydrator extends DoctrineObject {
        protected function handleTypeConversions($value, $typeOfField) 
        {
    
            if($typeOfField == 'datetime'){
                return \DateTime::createFromFormat('d/m/Y', $value);
            }
    
            return parent::handleTypeConversions($value, $typeOfField);
        }
    }
    

    It's make it simple to work with any date format. You can extend further making Locale assertions on this Custom Hydrator as you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能