0

Creating a date_select field in a custom form, like this:

$date_format = "Y-m-d";
$form['birth_date'] = array(
    '#type' => 'date_select',
    '#title' => t('Date of birth'),
    '#default_value' => NULL,
    '#date_type' => DATE_FORMAT_UNIX,
    '#date_timezone' => date_default_timezone(),
    '#date_format' => $date_format,
    '#date_increment' => 1,
    '#date_year_range' => 'now:1910',
    '#date_label_position' => 'within',
    '#required' => TRUE,
);

The element it generates for the year dropdown is like this

<select>
  <option value="0">-Year</option>
  <option value="1">2017</option>
  <option value="2">2016</option>
  <option value="3">2015</option>
   .
   .
   .
  <option value="108">1910</option>
</select>

The year value returned in $form_state['values'] is the value of the option, e.g. if 2017 is chosen the value returned is 1. Is it possible to somehow set the option values like this instead

<option value="2016">2016</option> 

so that the actual year will be returned?

1

This is how I get when I copied your code without any change. Option values are correct, but somehow the order got reversed.

<select class="date-year form-select" id="edit-birth-date-year" name="birth_date[year]">
    <option value="" selected="selected">-Year</option>
    <option value="1910">1910</option>
    <option value="1911">1911</option>
    <option value="1912">1912</option>
    .
    .
    .
    <option value="2017">2017</option>
</select>

Check your Date / Date API module versions. I'm using 7.x-2.8

1
  • I'm using 7.x-2.9. I rolled back to 7.x-2.8 and it works but as you mentioned, the order is reversed (that's easily corrected via hook_date_select_process_alter). Thanks for the hint - I'll follow up on d.o. – Frank H. Feb 28 '17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.