如何使用doctrine2中的日期字段类型规定年份范围 [英] How to stipulate a range of years using the date field type in doctrine2

查看:34
本文介绍了如何使用doctrine2中的日期字段类型规定年份范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Symfony2网站表单中下拉日期,并且正在我的表单构建器中使用日期"字段类型.这是我的声明:

I need a date drop down in a Symfony2 site form, and am using the 'date' field type in my form builder. Here is my declaration:

$builder->add('date', 'date', array(
   'label' => 'Date',
   'format' => 'dd MM yyyy',
   'required' => true
));

默认情况下,它将显示过去的5年和将来的5年(即2010年-2020年),但我需要从今年开始,并且只显示未来几年,就这种特殊形式而言不需要过去的日期或将来的日期.

By default this will display 5 years in the past and 5 years in the future (i.e. 2010 - 2020) but I need it to start from this year and only show a couple of years in the future, as for this particular form there is no need for a past date or a date too far in the future.

我阅读了《 Doctrine》文档中可以列出的年份范围,但是如何才能自动使用该年份以及将来的几年?

I read in the Doctrine documentation that you can list a range of years, but how I get it to automatically use THIS year and then a couple of years in the future?

谢谢:)

推荐答案

您可以使用 years 选项设置其格式:

You can format it using the years option:

$builder->add('date', 'date', array(
   'label' => 'Date',
   'format' => 'dd MM yyyy',
   'years' => range(date('Y'), date('Y') + 5),
   'required' => true
));

SF文档: http://symfony.com/doc/current/reference/forms/types/date.html#years

这篇关于如何使用doctrine2中的日期字段类型规定年份范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆