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

karens’s picture

Status: Active » Fixed

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

$form['value'] = array(
  '#tree' => TRUE,
  'mon' => array(
    '#type' => 'select'
  ...
  'year' => array(
    '#type => 'select',

Then the actual values in the form will look like:
name = "field_date[0]['value']['year']

karens’s picture

To 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.

nicolash’s picture

Cool, 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

// 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['date'] = array('#tree' => TRUE);
  $form['date'][0]['value'] = date_select_input($params);
  
  $params = array(
    'label' => t('To date'),
    'weight' => 2,
    'granularity' => array('M', 'D', 'Y'),
    'delta' => 1,
    'required' => 0,
  );

  $form['date'][0]['value2'] = date_select_input($params);

which puts out a form array, such as this...

Array
(
    [#tree] => 1
    [0] => Array
        (
            [value] => Array
                (
                    [#title] => From date
                    [#weight] => 1
                    [#theme] => date_form_select
                    [#attributes] => Array
                        (
                            [class] => container-inline-date
                        )

                    [mday] => Array
                        (
                            [#default_value] => 8
                            [#title] => day
                            [#required] => 1
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 2
                            [#size] => 2
                        )

                    [mon] => Array
                        (
                            [#default_value] => 8
                            [#title] => month
                            [#required] => 1
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 2
                            [#size] => 2
                        )

                    [year] => Array
                        (
                            [#default_value] => 2007
                            [#title] => year
                            [#required] => 1
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 4
                            [#size] => 4
                        )

                    [0] => Array
                        (
                            [#theme] => date_form_select_description
                            [#weight] => 11
                            [description] => Array
                                (
                                    [#value] => 
                                )

                        )

                )

            [value2] => Array
                (
                    [#title] => To date
                    [#weight] => 2
                    [#theme] => date_form_select
                    [#attributes] => Array
                        (
                            [class] => container-inline-date
                        )

                    [mday] => Array
                        (
                            [#default_value] => 8
                            [#title] => day
                            [#required] => 0
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 2
                            [#size] => 2
                        )

                    [mon] => Array
                        (
                            [#default_value] => 8
                            [#title] => month
                            [#required] => 0
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 2
                            [#size] => 2
                        )

                    [year] => Array
                        (
                            [#default_value] => 2007
                            [#title] => year
                            [#required] => 0
                            [#weight] => 
                            [#type] => textfield
                            [#maxlength] => 4
                            [#size] => 4
                        )

                    [0] => Array
                        (
                            [#theme] => date_form_select_description
                            [#weight] => 11
                            [description] => Array
                                (
                                    [#value] => 
                                )

                        )

                )

        )

)


that results in these form values...

 [date] => Array
        (
            [0] => Array
                (
                    [value] => Array
                        (
                            [mday] => 8
                            [mon] => 8
                            [year] => 2007
                        )

                    [value2] => Array
                        (
                            [mday] => 10
                            [mon] => 8
                            [year] => 2007
                        )

                )

        )

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

karens’s picture

There 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'.

nicolash’s picture

That's great, thanks very much again. I post a link to this in the handbook...

Anonymous’s picture

Status: Fixed » Closed (fixed)