4

I have a problem. When I put this code in my module checkboxes work great but form throws an error:

"The form has become outdated. Copy any unsaved work in the form below and then reload this page."

Alert one add node form.

function automobil_form_auto_node_form_alter(&$form, &$form_state, $form_id) {

  $form['add_phone'] = array (
    '#type' => 'fieldset',
    '#title' => t('Adauga telefon'),
    '#weight' => 6,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $form['add_phone']['phone'] = array(
    '#type' => 'phone',
    '#title' => t('Phone Number'),
    '#required' => TRUE,
  );

  $form['add_phone']['phone_submit'] = array (
    '#type' => 'button',
    '#value' => 'Add',
    '#prefix' => '<div class="col-md-6 text-left">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'automobil_add_phone_callback',
      'wrapper' => 'add-new-phone',
      'effect' => 'fade',
    ),
  );

  if(isset($form_state['values']['phone']['prefix'])) {
    $upload_users_phone = _upload_users_phone($form, $form_state, 'add');
  } else {
    $upload_users_phone = _upload_users_phone($form, $form_state, '');
  }

  $form['field_temporary_phone'] = array(
    '#prefix' => '<div id="add-new-phone">',
    '#suffix' => '</div>',
    '#title' => t('Telefon'),
    '#type' => 'checkboxes',
    '#weight' => 5,
    '#options' => $upload_users_phone,
  );
}

Ajax Callback

function automobil_add_phone_callback(&$form, &$form_state) {
  return $form;
}

Function that returns a list of user_phone that is stored in user profile. If a user adds new phone it adds it and return the updated list.

    function _upload_users_phone($form, $form_state, $operation, $returns) {
  global $user;
  // load user

  $existingUser = user_load($user->uid);

  if($operation == 'add') {
    $existingUser->field_user_phone['und'][]['value'] = $form_state['values']['phone']['prefix'] . ' ' . $form_state['values']['phone']['number'];
    $user = user_save((object) array('uid'=>$user->uid), (array) $existingUser);
  }
    // edit field-data
  foreach ($existingUser->field_user_phone['und'] as $key => $value) {
    $val_list[] = $value['value'];
    $key_list[] = $key;
  }

  return $val_list;
}

What is a problem (why is the 'form outdated form' error thrown)?

1

The reason you get the "The form has become outdated." error is because the token fails validation (meaning the old string doesn't match the new string). The token is determined by running a hash on several things including the session_id(). When you call user_save(), the session is changed/invalidated/whatever. Hence, when the token is tested next time around, it generates a different token because you have a different session_id() and therefore fails, thinking you were using an old form.

I have the same issue after calling user_save() from within my form processing. The way I fixed it feels like a pretty big hack, but I don't know that there's a clean way around it. The form code doesn't seem to expect that you would ever change/invalidate the session whilst in the middle of handling a form (in this case, a multi-part form).

Once you call user_save you must modify the form state manually to insert the updated token. Then, when it checks tokens your new token will match since you generated it after the 'session_id()' was updated.

So the resulting code looks like this. Notice that the input element is changed for $form_state to pass by reference so that the modification gets propagated up.

function _upload_users_phone($form, &$form_state, $operation, $returns) {
  global $user;
  // load user

  $existingUser = user_load($user->uid);

  if($operation == 'add') {
    $existingUser->field_user_phone['und'][]['value'] = $form_state['values']['phone']['prefix'] . ' ' . $form_state['values']['phone']['number'];
    $user = user_save((object) array('uid'=>$user->uid), (array) $existingUser);
    $form_state['input']['form_token'] = drupal_get_token($form['#token']);
  }
    // edit field-data
  foreach ($existingUser->field_user_phone['und'] as $key => $value) {
    $val_list[] = $value['value'];
    $key_list[] = $key;
  }

  return $val_list;
}

You might also need to set $form_state['values']['form_token'], but I'm pretty sure it's unnecessary. Again, don't forget the & in front of the declaration of the function variable $form_state or the value won't make it into the real array.

0

This happens, because somehow you're not using Form API correctly.

As for workaround, try adding form_token into your form, for example:

$form ['#token'] = $form_id;
$form ['form_token'] = array(
  '#id' => drupal_html_id('edit-' . $form_id . '-form-token'),
  '#type' => 'token',
  '#default_value' => drupal_get_token($form['#token']),
  '#parents' => array('form_token'),
);
}

and see if that works.

See also: Getting “The form has become outdated” error

0

The reason you get the "The form has become outdated." error is because the token fails validation (meaning the old string doesn't match the new string).

use unset($form['#token']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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