When the default value of a date_select element is not a string a warning is generated:
Warning: strlen() expects parameter 1 to be string, array given in date_default_date() (line 114 of \date\date_api\date_api_elements.inc).

Repeatable: always
Steps to repeat:
1. Create an element in a form of type date_select.
2. Set the default value of the element to an array containing correct date information.
3. Display the form.

Example:

// Part of a administrative settings form.
'time_from' => array(
  '#type' => 'date_select',
  '#title' => t('From'),
  '#date_format' => 'H:i:s a',
  '#default_value' => isset($settings['time_from'])
    ? $settings['time_from']
    : array('hour' => 7, 'minute' => 0, 'second' => 0, 'ampm' => 'am')
  ),
),

Expected Results:
Form displayed without any errors or warnings.

Actual Results:
A warning message is generated:
Warning: strlen() expects parameter 1 to be string, array given in date_default_date() (line 114 of \date\date_api\date_api_elements.inc).

Proposed resolution

There is a string length check in date_default_date() function in \date\date_api\date_api_elements.inc at line 114 which is the cause of the warning. Simply adding a type check should get rid of the problem.

User interface changes

User doesn't get a warning message (if errors are displayed).

API changes

None that I'm aware of.

None that I'm aware of.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pivanov’s picture

Status: Active » Needs review
FileSize
597 bytes

There is a string length check in date_default_date() function in \date\date_api\date_api_elements.inc at line 114 which is the cause of the warning. The DateObject used to parse the default value seems to accept an array and discards the format string.
Adding a type check should get rid of the warning.

pivanov’s picture

Issue summary: View changes

Correcting my mistake in the example code - date actually saves the time as a string rather than array.
As well the input format will be wrong if the value is taken from the settings. However, I'm not gonna fix the example at the moment.

pivanov’s picture

Issue summary: View changes

Removing duplicate "Issue Summary" heading.

pivanov’s picture

Issue summary: View changes

Fix a two obvious typos.

podarok’s picture

Assigned: Unassigned » vijaycs85
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

#1 nice catch, patch looks good for me
Good to see here test coverage
anyway for this part RTBC

podarok’s picture

Issue tags: +Needs tests
vijaycs85’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs tests
FileSize
2.49 KB
3.07 KB

Adding a test module and patch with just test module and with fix from #1

vijaycs85’s picture

Just adding missing new line at the end...

The last submitted patch, 4: 2038057-date-date_form_warning-4-test-only.patch, failed testing.

vijaycs85’s picture

#4 looks good. updating one more minor naming change.

podarok’s picture

possibly better here to use DrupalUnitTestCase, cause

$this->drupalGet('date-test/form');

just gets a page but not checks full workaround

+  if (is_string($element['#default_value']) && strlen($element['#default_value']) < 19) {

espessially due to

'#default_value' => array(
+      'hour' => 7,
+      'minute' => 0,
+      'second' => 0,
+      'ampm' => 'am'

in tests that newer do TRUE for this case cause $element['#default_value'] !is_string but array

podarok’s picture

Assigned: podarok » vijaycs85
Status: Needs review » Needs work
xeeshangulzar’s picture

The last submitted patch, 11: 2038057-test-only.patch, failed testing.

djdevin’s picture

Assigned: vijaycs85 » Unassigned
Status: Needs review » Reviewed & tested by the community
podarok’s picture

Status: Reviewed & tested by the community » Fixed

#11 merged. Thanks.

  • podarok committed e0c88c0 on 7.x-2.x authored by pivanov
    Issue #2038057 by vijaycs85, djdevin, pivanov: date_select element's...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.