ZF2从DateSelect元素中获取所选值

时间:2016-03-10 15:14:03

标签: php forms zend-framework element

在我的应用程序中,我使用DateSelect元素来接收用户的生日。当我通过选择生日值来测试表单时,我的字段保持为空,因为它应该给我一个日期格式。

我的表单代码;

$this->add(array(
            'name' => 'date_of_birth',
            'type' => 'DateSelect',
            'options' => array(
                'create_empty_option' => true,
                'max_year'            => date('Y') - 12,
                'day_attributes'      => array(
                    'data-placeholder' => 'Day',
                    'style'            => 'width: 31.5%',
                ),
                'month_attributes'    => array(
                    'data-placeholder' => 'Month',
                    'style'            => 'width: 31.5%',
                ),
                'year_attributes'     => array(
                    'data-placeholder' => 'Year',
                    'style'            => 'width: 31.5%',
                )
            ),
        ));

在我的视图中获取表单元素:

<?php echo $this->formRow($form->get('date_of_birth')); ?>

在我的控制器中测试输出:

..
Debug::dump($_POST);

.....
Debug::dump($form->getData());

$ _ POST返回;

 ....
 'day' => string '06' (length=2)
 'month' => string '05' (length=2)
 'year' => string '1999' (length=4)
 ...

但$ form-&gt; getData()会返回;

 ....
 'date_of_birth' => null
 ....

它应该给我一个date_of_birth填充日期,据我在互联网上阅读(blog)。我做错了什么?

1 个答案:

答案 0 :(得分:0)

到目前为止,我使用下面给出的解决方法,如果有人想出如何以正确的方式管理它,请告诉我!

$aPostData = $request->getPost();
  if ($request->getPost('year') && $request->getPost('month') && $request->getPost('day')) {
    $dateTimeFormat = new \DateTime(date('Y-m-d', strtotime($request->getPost('year') . '-' . $request->getPost('month') . '-' . $request->getPost('day'))));
    $aPostData['date_of_birth'] = $dateTimeFormat->format('Y-m-d');
  }
$form->setData($aPostData);
相关问题