如何在 Symfony2 中显示月-年下拉列表 [英] How to display a month-year dropdown in Symfony2
本文介绍了如何在 Symfony2 中显示月-年下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,用户 hsa 仅通过在两个下拉列表中选择月份和年份来给出日期.我怎样才能做到这一点?
In my application, the user hsa to give a date by only selecting a month and a year in two dropdown lists. How can I achieve that?
这是我迄今为止尝试过的:
Here is what I've tried so far :
在我的表单中:
$builder->add('date1', 'date', array(
'widget' => 'choice',
'empty_value' => '',
'format' => 'MMMM-yyyy',
'input' => 'datetime',
'years' => range(date('Y'), date('Y') - 30, -1)
)
它实际上可以正常工作并完全按照我的意愿显示它,但是在验证表单时,我收到一个错误:
which actually works and displays it exactly as I want but when validating the form, I get an error :
This value is not valid
想要的结果截图:
推荐答案
信用卡有效期示例:
$builder->add('expirationDate', 'date', array(
'label' => 'Expiration date',
'widget' => 'choice',
'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
'format' => 'dd-MM-yyyy',
'input' => 'string',
'data' => date('Y-m-d'),
'years' => range(date('Y'), date('Y') + 10),
));
那么你必须手动渲染这个字段.
Then you must render this field manually.
表单的树枝模板:
{{ form_row(form.expirationDate, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">/</span> {{ year }}'}) }}
覆盖 date_pattern
将隐藏日期选择.您将获得月/年格式.
Overriding date_pattern
will hide day select. You will get month / year format.
这篇关于如何在 Symfony2 中显示月-年下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文