I'm having a whole mess of issues adding text fields to a form in Drupal 6. I want to add a text field with an autocomplete. I tried adding the field in hook_form_alter() like so:
$form['my_field_name'] = array(
'#type' => 'textfield',
'#title' => 'My Title',
'#required' => true,
'#default_value' => 'my_default_value',
'#autocomplete_path' => 'url/to/autocomplete'
);
$output .= drupal_render($form['my_field_name']);
When I output this, the field output to html does not have an id or name attribute, and the autocomplete doesn't work (presumably because the id is missing. Additionally, the default value is not populated in the field.
I can overcome some of these issues by adding in a custom #id, #name, and #value, like so:
$form['my_field_name'] = array(
'#type' => 'textfield',
'#title' => 'My Title',
'#id' => 'my_field_id',
'#name' => 'my_field_name',
'#required' => true,
'#value' => 'my_default_value',
'#autocomplete_path' => 'url/to/autocomplete'
);
$output .= drupal_render($form['my_field_name']);
This gets the autocomplete working as expected, gives a name and id attribute, and fills in the default value, but unfortunately now the autocomplete does not actually change the value of the field, so I can't enter and submit a new value.
How can I overcome these problems with form_alter?