If you are passing a date part (day, month or year) to the date form element which contains a leading zero, it will incorrectly set the form element.
For example, say you pass a default date value in a module like this :
$form['date']['#default_value'] = array('day' => '20', 'month' => '04', 'year' => '1994')
It will incorrectly set the month select box to the default value of January.
I made a small quick patch which adds two lines to the expand_date function in form.inc which does seem to fix the problem by explicitly type casting the date parts for #value and #default_value to integers, though the patch probably could be improved upon.
Comments
Comment #1
gdevlugt commentedI forgot to mention this isn't strictly Drupal 5.2 related. Version 5.1 seems to exhibit the same behaviour.
Comment #2
gdevlugt commentedOops, the diff file is incorrect. Attached a correct version.
Comment #3
drummThe indentation needs to be fixed.
And is there anything particularly bad with requiring module code to properly cast a variable?
Comment #4
gdevlugt commentedFixed the indentation.
Since the input which the date form element requires is always numeric, putting the cast in the core code is more elegant and convenient. This way you don't have to cast the values the element receives in multiple places in your modules code (DRY principle).
Comment #5
Christoph Petschnig commentedI came here in search for a previous bug report, before I wanted to report my own bug (fix).
In function expand_date in includes/form.inc:
The bug affects dates in the form '02' to '09' (days 2 to 9 in the months Februar until September). There will be no "selected"-tag on these months/days in the select box.
The affected code looks a little different in 5.6, though. The expand_date function is still unchanged in 6.0-rc3. Somebody should fix this for 6.0-rc4 and 5.8.
This causes really bad behaviour i.e. in module Advanced Poll.
Comment #6
gábor hojtsyWell, I don't think this is a bug at all. The form API recognizes values when you specify them as they are in the options in the form. Make sure you convert/cast however it needs to be to get the same value. Feel free to discuss this more in Drupal 7.
Comment #7
Christoph Petschnig commentedOk, you may be right. Still, you have to be very careful to write
instead of:
I still think this should be changed, as SQL makes no difference between those types of dates.
Comment #8
zahor commentedComment #9
catchI'm not convinced either way on this, but it needs a code comment to explain the cast to int and probably a test too.
Comment #10
marcingy commentedBumping to d8 the issue still applies.
Comment #11
marcingy commentedAdding tag
Comment #14
jhedstromClosing due to inactivity, and the fact that not everybody agreed this to be a bug. Feel free to reopen if there is an actionable fix (if it's even still an issue).