0

I have a profile2 regpath displaying my profile2 form, and the system default account registration form below it. I need to move the account email field up into the container with the profile2 fields.

enter image description here

I have altered the form and moved the account mail field from the account form, into the profile form, and I have created an additional validation handler to try insert the mail value into the $form_state value so the user_account_form_validate() doesn't choke on a missing ['mail'] index:

function si_student_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  // remove fieldgroups and tabs for anonymous users, e.g. /student/register
  if($user->uid == 0) {
    // move email field from account to below profile2 name fields
    $email = $form['account']['mail'];
    unset($form['account']['mail']);
    // add email to profile
    $form['profile_student']['mail'] = $email;
    // change weights
    $form['profile_student']['mail']['#weight'] = 3;

    // custom validation handler to inject mail value into 
    // correct place in $form_state['values'] array
    $form['#validate'][] = '_si_student_register_anonymous_validate';
    //dpm($form);
  }
}

function _si_student_register_anonymous_validate($form, &$form_state) {
  $form_state['values']['mail'] = $form_state['values']['profile_student']['mail'];
}

However this isn't working, and I get the following:

enter image description here

Any ideas? I'm not sure I'm taking the right approach here. Thanks

1 Answer 1

0

Found solution here: move fields in the user profile form in fieldsets

But I also needed to add the submitted $form_state mail value, back to the location it would have originally existed.

function si_student_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  // remove fieldgroups and tabs for anonymous users, e.g. /student/register
  if($user->uid == 0) {

    // move email field from account to below profile2 name fields
    $email = $form['account']['mail'];
    unset($form['account']['mail']);
    // add email to profile
    $form['profile_student']['mail'] = $email;
    // change weights
    $form['profile_student']['mail']['#weight'] = 3;

    // replace original validation handler
    $form['#validate'][0] = '_si_student_register_anonymous_validate';
  }
}

function _si_student_register_anonymous_validate(&$form, &$form_state) {

  // add field back to account
  $form['account']['mail'] = $form['profile_student']['mail'];
  // add submitted value from profile2 submission handler into default mail value location
  $form_state['values']['mail'] = $form_state['values']['profile_student']['mail'];
  // call validation function with update field
  user_account_form_validate($form, $form_state);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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