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)?