Custom fields added via form api not working as expected 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' =&gt; 'textfield',
  '#title' =&gt; 'My Title',
  '#required' =&gt; true,
  '#default_value' =&gt; 'my_default_value',
  '#autocomplete_path' =&gt; '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' =&gt; 'textfield',
  '#title' =&gt; 'My Title',
  '#id' =&gt; 'my_field_id',
  '#name' =&gt; 'my_field_name',
  '#required' =&gt; true,
  '#value' =&gt; 'my_default_value',
  '#autocomplete_path' =&gt; '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?
AFAIK, hook_form_alter() does not require you to perform a drupal_render() in it. And I suspect that is why you are not getting a proper rendered for item.

Also, each custom field needs to processed (by you) on form submit.
Thus, you need to add $form['#submit'][] = 'my_submit_function_submit';, where my_submit_function_submit() handles the database inserts.