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

gdevlugt’s picture

I forgot to mention this isn't strictly Drupal 5.2 related. Version 5.1 seems to exhibit the same behaviour.

gdevlugt’s picture

StatusFileSize
new419 bytes

Oops, the diff file is incorrect. Attached a correct version.

drumm’s picture

Priority: Normal » Minor
Status: Needs review » Needs work

The indentation needs to be fixed.

And is there anything particularly bad with requiring module code to properly cast a variable?

gdevlugt’s picture

Status: Needs work » Needs review
StatusFileSize
new420 bytes

Fixed 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).

Christoph Petschnig’s picture

Version: 5.2 » 6.0-rc3
Status: Needs review » Reviewed & tested by the community

I 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:

1666c1666
<       '#value' => $element['#value'][$type],
---
>       '#value' => intval($element['#value'][$type]),

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.

gábor hojtsy’s picture

Version: 6.0-rc3 » 7.x-dev
Status: Reviewed & tested by the community » Needs review

Well, 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.

Christoph Petschnig’s picture

Ok, you may be right. Still, you have to be very careful to write

$form['my_date'] = array(
  '#type' => 'date',
  '#default_value' => array('year' => date('Y', $date), 'month' => date('n', $date), 'day' => date('j', $date)),
);

instead of:

$form['my_date'] = array(
  '#type' => 'date',
  //  will fail on days/month from '02' to '09'
  '#default_value' => array('year' => date('Y', $date), 'month' => date('m', $date), 'day' => date('d', $date)),
);

I still think this should be changed, as SQL makes no difference between those types of dates.

zahor’s picture

catch’s picture

Status: Needs review » Needs work

I'm not convinced either way on this, but it needs a code comment to explain the cast to int and probably a test too.

marcingy’s picture

Version: 7.x-dev » 8.x-dev

Bumping to d8 the issue still applies.

marcingy’s picture

Issue tags: +Needs backport to D7

Adding tag

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

Closing 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).