【发布时间】:2013-02-03 01:36:20
【问题描述】:
随着新发布的 Zend Framework 2 版本,two new form elements were added; DateSelect 和 MonthSelect。我想使用前者,它增加了三个选择;日、月、年。但是,我在按我想要的方式格式化输出时遇到问题 - 而且,没有这方面的文档!
我正在像这样将表单元素添加到我的表单中(取自this page):
$this->add(array(
'type' => 'Zend\Form\Element\DateSelect',
'name' => 'birthDate',
'options' => array(
'label' => 'Date',
'create_empty_option' => true,
'day_attributes' => array(
'data-placeholder' => 'Day',
'style' => 'width: 20%',
),
'month_attributes' => array(
'data-placeholder' => 'Month',
'style' => 'width: 20%',
),
'year_attributes' => array(
'data-placeholder' => 'Year',
'style' => 'width: 20%',
)
)
));
为了输出,我是这样做的:
echo $this->formDateSelect($form->get('birthDate'));
问题是我不知道如何控制选择的格式。例如,它们当前以这种形式输出:[month] [day], [year]。我想改变两件事;首先,我想切换日期和月份,其次,我想去掉逗号。
助手使用IntlDateFormatter 类来格式化选择。在源代码中,我注意到在调用__invoke() [link to source code] 时可以使用predefined constants 作为日期类型,如下所示:
echo $this->formDateSelect($form->get('birthDate'), IntlDateFormatter::SHORT);
尝试了所有的常量,我仍然无法得到我想要的格式。
我注意到三个选择都有 getter,但是对每个选择使用 formSelect 视图助手时,会呈现空选择,因为数据填充在新的视图助手中。所以这几乎违背了单独使用选择的目的;那么我不妨只使用常规选择。
有谁知道如何更好地控制我的选择的格式?是否有可能没有太多麻烦,或者我应该只使用三个常规选择以获得最大的灵活性?
【问题讨论】:
标签: php zend-form zend-framework2 zend-form-element