Closed (fixed)
Project:
Date
Version:
5.x-1.3
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
7 Aug 2007 at 04:04 UTC
Updated:
23 Aug 2007 at 12:19 UTC
I'm trying to get a from and to date using Date API in a custom form with FAPI, as in the example here. That works fine, but if I use a second field with a different delta, the field names end being the same as the previous (mday, mon, year), simply overwriting the previous values.
Looking at the date_text_input() function, I can't see how it would use the delta at all to distinguish several fields, but maybe I'm missing something.
Here's the code...
// include the API...
include_once(drupal_get_path('module', 'date_api') .'/date.inc');
$params = array(
'label' => t('From date'),
'weight' => 1,
'granularity' => array('M', 'D', 'Y'),
'delta' => 0,
// more params can be set according to documentation in date_select_input().
);
$form['from_date'] = date_select_input($params);
$params = array(
'label' => t('To date'),
'weight' => 2,
'granularity' => array('M', 'D', 'Y'),
'delta' => 1,
);
$form['to_date'] = date_select_input($params);
Comments
Comment #1
karens commentedYou have to put the collection into a higher level element with a distinct name for the from and to values. The date module uses 'value' and 'value2'. Then to keep the form from collapsing down to just 'mon' and 'year', you need to set '#tree' => TRUE on the top level. Do a print_r() on the $form created by the date module and you'll see that it looks something like:
Then the actual values in the form will look like:
name = "field_date[0]['value']['year']
Comment #2
karens commentedTo be more clear, you'll get the part below the 'value' from calling the function. You have to put the array you receive back into an array so it ends up as I illustrated above. And I see you're using a textfield, not a select, so your array looks a bit different, but it's the same concept.
Comment #3
nicolash commentedCool, got it, thanks Karen! So really, the 'delta' that's mentioned in the function documentation isn't really responsible for this...
So what I ended up with is
which puts out a form array, such as this...
that results in these form values...
Maybe we could put a link to this from the example handbook page, since the approach is quite different from what's descibed there?
One more question...I'm not using textfields for the dates by choice, it seems to be the default. How can I change them to select lists?
Thanks
Nick
Comment #4
karens commentedThere are lots of options that are not well documented:
$params['select_month'] - should the month be a select(1) or textfield (0)
$params['select_day'] - should the day be a select(1) or textfield (0)
$params['select_year'] - should the year be a select(1) or textfield (0)
$params['years_back'] - number of years to go back in the year selector
$params['years_forward'] - number of years to go forward in the year selector
$params['increment'] - sets the increment to use for minutes and seconds, defaults to 1
The methods for doing this are going to change in the 5.2 version of the Date module, so I'm not going to change the documentation until I change it for the 5.2 changes. In 5.2 we're using FAPI standards, so you'll set those things in the $form array (like '#date_increment => 15), the set the #type to 'date_select' or 'date_textfield'.
Comment #5
nicolash commentedThat's great, thanks very much again. I post a link to this in the handbook...
Comment #6
(not verified) commented