0

Hi I'm trying to save a field into another field in the same form when the form is submitted.

I've tried using hook_form_alter and checked for count($form_state['input']) > 0 to save $form['account']['field_first_name'] into $form['profile']['field_first_name']

I know you may say that I can use the profile2 module. I'm already using that. I just need to figure out how to save one field value into another field when a form is submitted.

Any help would be greatly appreciated.

1

Got it! In my hook_form_alter function I added $form['#submit'][]='my_submit';

Then I created the my_submit() function.

function my_submit($form, &$form_state) {
  if (count($form_state['input']) > 0) {
    form_state_values_clean($form_state);
    $form_state['values']['profile']['field_first_name'] = $form_state['values']['field_first_name'];
    $form_state['values']['profile']['field_last_name'] = $form_state['values']['field_last_name'];
    $form_state['values']['profile']['field_groups'] = $form_state['values']['field_groups'];

    unset($form_state['values']['field_first_name']);
    unset($form_state['values']['field_last_name']);
    unset($form_state['values']['field_groups']);
  }
}

The important parts of the function above to notice are form_state_values_clean($form_state); then I access the values through $form_state['values'] array.

Hope this helps someone else looking to do the same functionality.

| improve this answer | |

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.