I'm trying to use the date_popup element with just the time portion, to allow users to enter a time value in a custom form.
The docs at the top of date_popup.module say this ought to be doable: "If no date elements are included in the format string, only the time textfield, will be created."
Here's my form code:
$form['create_performances']['time'] = array(
// Use the jQuery date_popup type so the widget is nice to use.
'#type' => 'date_popup',
'#title' => t('Default performance time'),
'#description' => t("Select a time for all performances."),
'#date_format' => 'H:i',
'#date_text_parts' => array('hour', 'minute'),
'#default_value' => '2011-01-01 19:30:00',
);
This makes the element correctly in the form, complete with a default time. So far so good.
But on submission, the $form_values has a NULL for this element.
I believe the problem is this part of date_popup_validate():
if (empty($element['#value']['date'])) {
if ($element['#required']) {
// Set message on both date and time to get them highlighted properly.
$message = t('Field %field is required.', array('%field' => $label));
if (!empty($date_granularity)) {
form_set_error($error_field .'][date', $message);
$message = ' ';
}
if (!empty($time_granularity)) {
form_set_error($error_field .'][time', $message);
}
}
dsm('setting it to NULL...');
form_set_value($element, NULL, $form_state);
return;
}
Because there is no date part of the value, the whole element gets a NULL.
Comment | File | Size | Author |
---|---|---|---|
#21 | date_popup-time_only_submit-1037150-21.patch | 2.14 KB | antroxim |
Comments
Comment #1
jlscott CreditAttribution: jlscott commentedI have the same error, and found a solution by making a change in "date_popup.module", function "date_popup_validate".
The test in line 374:
needs to be replaced with
and the test on line 431
should be replaced with
I haven't tested whether there are any problems in CCK date fields as a result of this change, as I don't use CCK.
Comment #2
raggax2 CreditAttribution: raggax2 commentedI have the same need of just needing a time field without the date. I tested your suggestion out with the most current branch of date. This code starts to break down during the date_make_date. The result of just the time being in the value array is that the ultimate $value ends up being 0000-00-00 01:34 (for example). When you pass that through the date_make_date function ends up with a funky date like -01-34-0001.
This issue seems to be apart of a larger feature request at http://drupal.org/node/127200
I think this proposal is on the right path, but it seems like recent changes to the module may have broken them.
Comment #3
raggax2 CreditAttribution: raggax2 commentedI've attached a patch based on the code recommended by jlscott. It basically only adds some extra logic to the branch statements in the validation process. All current simpletests pass on my local environment. One of the weird things about this issue is that we are validating time against a date object. This patch does end up producing a date like the one mentioned in comment #2. However, if you are doing a format_date() or something similar, the invalid date may not matter. There may be some additional processing needed to make this patch production worthy.
Comment #4
bartl CreditAttribution: bartl commentedI'm having the same problem and luckily I went to google for it, so I found this thread...
I like the original solution #1 better than the patch in #3, because the latter ignores granularity. That is essential.
To solve the problem will the zero date, I'd prefer to copy the original date (and/or time) from the default value if the only granularity present, is time.
One possible solution may be, when the element is processed, in this case, to have the date alone stored as a value, instead of just dropped.
You'd still get an error if there is no default date...
Comment #6
raggax2 CreditAttribution: raggax2 commentedI don't necessarily think that the submitted patch ignores granularity. I think granularity is already taken into consideration in the nested ifs:
They way I understand the code is that if either the time or date fields are empty and the field is required, then set the error message. Granularity is taken into consideration on the actual set errors. I don't see how adding the granularity in the initial branch test would gain you anything in terms of what the block is actually executing. I've included an updated patch to the most current version of the 6.x branch. I've updated a little bit of the branch logic around line 459. I have not yet included basing the time / date components on the #default_values.
As to the 2nd suggestion of setting the element value based on the #default_value components, that is an interesting idea. Is there any feedback from the module maintainers for this suggestion?
Comment #7
vishy_singhal CreditAttribution: vishy_singhal commenteduse the following property of the type date_popup
as
'#dateonly' => TRUE
or/and
'#timeonly' => TRUE
Comment #8
joachim CreditAttribution: joachim commentedThanks, will try it!
(Fixing title)
Comment #9
vishy_singhal CreditAttribution: vishy_singhal commentedSorry, forgot to mention the following integration of code as well.
Version 7.x-2.6
Drupal 7.20
File date_popup.module
Function - date_popup_validate
Comment #10
joachim CreditAttribution: joachim commentedCould you post that as a patch please?
Comment #11
vishy_singhal CreditAttribution: vishy_singhal commentedI definitely can. Will do it within a few days.
Comment #12
vishy_singhal CreditAttribution: vishy_singhal commentedSubmitting patch as requested.
Comment #14
vishy_singhal CreditAttribution: vishy_singhal commentedCan you post results of test
Comment #15
Risse CreditAttribution: Risse commentedI am still getting this bug on the latest 7.x-2.x version.
The previous patch fixes the bug, here it is modified to work on the latest version.
Comment #16
Risse CreditAttribution: Risse commentedComment #18
vijaycs85Comment #19
antroxim CreditAttribution: antroxim at Drupal Ukraine Community for Drupal Ukraine Community commentedComment #20
antroxim CreditAttribution: antroxim at Drupal Ukraine Community for Drupal Ukraine Community commentedAll patches should be tested against dev version.
Comment #21
antroxim CreditAttribution: antroxim at Drupal Ukraine Community for Drupal Ukraine Community commentedSubmitting a fixed patch from #15 for latest dev version
Comment #24
podarokThanks, merged