How to stipulate a range of years using the date field type in doctrine2

て烟熏妆下的殇ゞ 提交于 2021-02-05 06:47:45

问题


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
));

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.

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?

Thank you :)


回答1:


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 docs: http://symfony.com/doc/current/reference/forms/types/date.html#years




回答2:



You can use years instead year it works like that for me.

$builder->add('date', 'date', array(
   'label' => 'Date',
   'years' => range(date('Y'), date('Y') + 10),
   'required' => true
));

Or specified date,
for example between 1990 and today:

$builder->add('date', 'date', array(
       'label' => 'Date',
       'years' => range(date('1990'), date('Y')),
       'required' => true
    ));

Symfony 2, Doctrine 2.4.8



来源:https://stackoverflow.com/questions/33810261/how-to-stipulate-a-range-of-years-using-the-date-field-type-in-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!