Add a hidden field on form submit I am working on a login form (information sent via https to a remote system), that by default contains a username field, a password field and a static hidden field (necessary in order to validate with the remote system).

There is one additional hidden field necessary, which should be built from the value of the username and the value of the existing hidden validation field, separated by an underscore, like this: username_hidden_id

Is it possible to achieve that after the form has been submitted? I tried to add the field using the form submit function, but that doesn't seem to work.

I could add this the mentioned field and generate it's value via JavaScript/jQuery, but I wouldn't like to have this field visible/public.

I have the following code:

function custom_login_form($form, $form_state) {

    $form['username'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Username'),
      '#maxlength' =&gt; USERNAME_MAX_LENGTH,
      '#size' =&gt; 15,
      '#required' =&gt; TRUE,
    );
    $form['password'] = array(
      '#type' =&gt; 'password',
      '#title' =&gt; t('Password'),
      '#size' =&gt; 15,
      '#required' =&gt; TRUE,
    );
    // Hidden validation field    
    $form['hidden_id'] = array(
      '#type' =&gt; 'hidden',
      '#value' =&gt; '1234',
    );
    // Submit button
    $form['actions'] = array('#type' =&gt; 'actions');
    $form['actions']['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Log in'),
    );

    return $form;
}

function custom_login_form_validate(&amp;$form, &amp;$form_state) {

  if (empty($form_state['values']['username'])) {
    form_set_error('username', 'Please enter a username.');
    $form_state['rebuild'] = TRUE; 
  }
  if (empty($form_state['values']['password'])) {
    form_set_error('password', 'Please enter a password.');
    $form_state['rebuild'] = TRUE; 
  }

}

function custom_login_form_submit(&amp;$form, &amp;$form_state) {

  if (isset($form_state['input']) &amp;&amp; !empty($form_state['input']['username']) &amp;&amp; !empty($form_state['input']['hidden_id'])) {
    $form['username_hidden_id'] = array(
      '#type' =&gt; 'hidden',
      '#value' =&gt; $form_state['values']['username'] . '-' . $form_state['values']['hidden_id'],
    );
  }

}
You don't want to modify the $form array in the _submit function. Rather, you should modify the $form_state['values'] array, as that's where all submitted values are stored. So:

function custom_login_form_submit(&amp;$form, &amp;$form_state) {
  if (isset($form_state['input']) &amp;&amp; !empty($form_state['input']['username']) &amp;&amp; !empty($form_state['input']['hidden_id'])) {
    $form_state['values']['username_hidden_id'] = $form_state['values']['username'] . '-' . $form_state['values']['hidden_id'];
  }
}


Note that you can also do all of the above in the _validate function, and $form_state['values']['username_hidden_id'] will then be available in the _submit function.

Why do you need it to act like a submitted field, though? I would just save it as a variable and use it as needed, like so:

function custom_login_form_submit(&amp;$form, &amp;$form_state) {
  if (isset($form_state['input']) &amp;&amp; !empty($form_state['input']['username']) &amp;&amp; !empty($form_state['input']['hidden_id'])) {
    $username_hidden_id = $form_state['values']['username'] . '-' . $form_state['values']['hidden_id'];
  }
  // Do something with $username_hidden_id here.
}