//make a texfield
$form['gameAwayTeam'] = array (
'#type' => 'select',
'#title' => t('Away Team'),
'#options' => $teamName,
'#default_value' => 'test',

);

This is the code in the module i developed. Not sure why but the default value is not being recognized. Has anyone had this problem in the past. is this a bug?

as a work around I just added the value I needed to the beginning of the array with this php function

array_unshift($teamName,'defaultValueHere');

But I'd like to know why the default value isn't being taken.

Thanks,
Mike

Comments

Jaypan’s picture

How is $teamName structured?

mikebann’s picture

It's an array I build from a database call. Other then it being an array what do you mean structured?

Jaypan’s picture

Is it a keyed array? Or a numerical index?

mikebann’s picture

It's keyed I'm defining a specific key for the array so that the value of the array represents the displayed option. Is that the problem? How does drupal deal with an arrays keys.

prakashp’s picture

This should work

  $teamName = array(
    'team_a' => 'Team A',
    'test' => 'Test',
  );
  $form['gameAwayTeam'] = array (
    '#type' => 'select',
    '#title' => t('Away Team'),
    '#options' => $teamName,
    '#default_value' => 'test',
  );
mikebann’s picture

Does this mean that the default value is actually referring to the key of the array. I thought it was just adding a new entry. I see why I'm confused now.

Thanks!

Jaypan’s picture

That's exactly it. You enter the key of the element that you want to be selected.

nevets’s picture

I have noticed that with Firefox the problem lies with the browser. If you look at the html the default is set correctly but not displayed by the browser.