By itchy on
I am using the date element in my form. I would like to change the range for the years. The default is 1900 to 2050 as coded in expand_date(). How can I change the year range?
I am using the date element in my form. I would like to change the range for the years. The default is 1900 to 2050 as coded in expand_date(). How can I change the year range?
Comments
I realise this might not be
I realise this might not be the best solution for this problem, but it's the only one I've been able to make work. It consists of tweaking the select element in case it's a year combobox. Since I want all years to range from the present to 2006, then it works for my entire site. Hope it helps.
Thanks. I also found another solution...
In my hook_form() function I set the #after_build property for the date element. I set this value as an array with the name of my call back function.
The #after_build property executes the array of functions names after the form has been constructed at which point I need to replace the [year][#options] property value of my date element to my new range. The parameter $form_element is the closing_date element from my form (as defined above).
This works fine for what I need. I'll take a closer look at your solution as I'm sure there is some new things that I can learn from it. Thanks a lot for replying.
Personnalize date range
it just a compliment to the solution above ...
from Morocco...
Thanks, this is sensible
I was approaching this problem by overriding $element['date']['#process'] in hook_elements() to point to a customized version of the expand_date() function, but that didn't seem quite right. This is definitely less klugey. I tweaked the function so that the date range defaults to the 25 years before and after the current year, but uses the element's '#year_start' and '#year_end' properties if present, so you can customize individual date elements as necessary:
Slight change to this for upward compatibility
Great solution. I noticed that #start_year and #end_year properties have been committed to CVS for version 7 see #47380: Date form element years range, so you may like to make a small change to reduce growing pains: (just flipping year_start around so it becomes start_year)
thanks
thanks,
very helpful solution worked absolutely fine for me.
Was looking for this...
I needed to limit the date range in my form and your solution worked perfectly.
Thank you.
Teja
Great! Thanks!
Itchy's solution worked really well for me too!
Webform module has an elegant solution
The webform module adds a useful function for processing the date field, adding '#year_start' and '#year_end' elements to the date field. You can either put actual start and end years in these fields, or use + and - for offsets from the current year.
If you have this module active, you could call the webform function in your form creation as follows:
If you don't have the Webform module on your site, I've copied a trimmed down version of the functions below (just change "my_module" to your module name):
An example of its use is:
Hope this helps somebody.
James
thx
definitely helped me
It think there is a small bug in the code.
The function name should be function my_module_expand_date($element) and not with '_pricing_'
Thx once again !
Use #date_year_range
A better way to do this:
date_popup date range
If use date_popup, changing year range:
Select first year, mouth and date.