3

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' => 'textfield',
      '#title' => t('Username'),
      '#maxlength' => USERNAME_MAX_LENGTH,
      '#size' => 15,
      '#required' => TRUE,
    );
    $form['password'] = array(
      '#type' => 'password',
      '#title' => t('Password'),
      '#size' => 15,
      '#required' => TRUE,
    );
    // Hidden validation field    
    $form['hidden_id'] = array(
      '#type' => 'hidden',
      '#value' => '1234',
    );
    // Submit button
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Log in'),
    );

    return $form;
}

function custom_login_form_validate(&$form, &$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(&$form, &$form_state) {

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

}
2
  • possible duplicate of Autofilling fields based on another field
    – Mołot
    Commented Aug 28, 2013 at 20:05
  • Not sure if I agree that this is a dup. The linked question already has the hidden field to be updated, this one doesn't. So, it is a slightly difference scenario.
    – mpdonadio
    Commented Sep 7, 2013 at 12:46

1 Answer 1

1

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(&$form, &$form_state) {
  if (isset($form_state['input']) && !empty($form_state['input']['username']) && !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(&$form, &$form_state) {
  if (isset($form_state['input']) && !empty($form_state['input']['username']) && !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.
}
2
  • 1
    I met the same situation with some API when a SHA signature had to be sent along the form. However that signature was calculated based on the other form inputs, so it had to be calcultated after the user clicking the Submit button and it had to be sent as a form field.
    – Cédric D
    Commented Jun 11, 2014 at 9:05
  • @CédricD Could you explain how did you manage to send data with post method after the user clicked the Submit button ? Commented Jul 9, 2019 at 15:03

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