在我的应用程序中,我使用 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->getData() 返回;
....
'date_of_birth' => null
....
它应该给我一个充满日期的date_of_birth,就我在互联网(博客)上阅读而言。我做错了什么?
到目前为止,我使用下面给出的解决方法,如果有人想出如何以正确的方式管理它,请告诉我!
$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);