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.
Related Issues
None that I'm aware of.
Comment | File | Size | Author |
---|---|---|---|
#1 | date-element-default-value-2038057-1.patch | 597 bytes | pivanov |
#4 | 2038057-date-date_form_warning-4-test-only.patch | 2.49 KB | vijaycs85 |
#11 | date_select_element_s-2038057-11.patch | 3.05 KB | djdevin |
#11 | 2038057-test-only.patch | 2.47 KB | djdevin |
Comments
Comment #1
pivanov CreditAttribution: pivanov commentedThere 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.
Comment #1.0
pivanov CreditAttribution: pivanov commentedCorrecting 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.
Comment #1.1
pivanov CreditAttribution: pivanov commentedRemoving duplicate "Issue Summary" heading.
Comment #1.2
pivanov CreditAttribution: pivanov commentedFix a two obvious typos.
Comment #2
podarok#1 nice catch, patch looks good for me
Good to see here test coverage
anyway for this part RTBC
Comment #3
podarokComment #4
vijaycs85Adding a test module and patch with just test module and with fix from #1
Comment #5
vijaycs85Just adding missing new line at the end...
Comment #7
vijaycs85#4 looks good. updating one more minor naming change.
Comment #8
podarokpossibly better here to use DrupalUnitTestCase, cause
just gets a page but not checks full workaround
espessially due to
in tests that newer do TRUE for this case cause $element['#default_value']
!is_string
butarray
Comment #9
podarokComment #10
xeeshangulzar CreditAttribution: xeeshangulzar commented7: 2038057-date-date_form_warning-7.patch queued for re-testing.
Comment #11
djdevinThis still happens, re-rolling.
Comment #13
djdevinComment #14
podarok#11 merged. Thanks.