2

I want to move the email address fields in the user profile form (user/1/edit) in a custom fieldset. I have this code:

$form['change_mail'] = array(
    '#type' => 'fieldset',
    '#title' => 'change email address cake',
    '#weight' => 6,
    '#collapsible' => FALSE,
    '#tree' => FALSE,
);

$form['change_mail']['mail'] = $form['account']['mail'];
unset($form['account']['mail']);
$form['change_mail']['confirm_mail'] = $form['account']['confirm_mail'];
unset($form['account']['confirm_mail']);

But I get an error when I try to change my email address: undefined index 'mail' in user_account_form_validate() (line 1232 of /modules/user/user.module). Apparently, I can't 'unset' the fields. But I don't know how to get rid of them.

2
  • try setting $form['change_mail']['#tree'] = false. Some info here drupal.org/node/48643
    – mojzis
    Mar 27, 2013 at 20:53
  • isn't that what my code did? $form['change_mail'] = array( ... '#tree' => FALSE,);
    – Pax
    Mar 27, 2013 at 21:12

1 Answer 1

4

As you mentioned the problem appears in user_account_form_validate() because you changed the account index from $form['account']['mail'] to $form['change_mail']['mail']

the issue is with this line form_set_value($form['account']['mail'], $mail, $form_state);

In order to achieve what you are looking for you need to replace existing form validation with a custom validation as the following code:

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

  $form['change_mail'] = array(
    '#type' => 'fieldset',
    '#title' => 'change email address cake',
    '#weight' => 6,
    '#collapsible' => FALSE,
    '#tree' => FALSE,
  );

  $form['change_mail']['mail'] = $form['account']['mail'];
  unset($form['account']['mail']);
  $form['change_mail']['confirm_mail'] = $form['account']['confirm_mail'];
  unset($form['account']['confirm_mail']);


  $form['#validate'][0] = 'MODULE_NAME_user_account_form_validate';
}


function MODULE_NAME_user_account_form_validate($form, &$form_state) {
  $form['account']['mail'] = $form['change_mail']['mail'];
  // original validation function
  user_account_form_validate($form, $form_state);
}
4
  • Why do you put $form['#validate'][0] = 'MODULE_NAME_user_account_form_validate'; if you're adding a validation function? Isn't it supposed to be $form['#validate'][] = 'MODULE_NAME_user_account_form_validate';
    – Pax
    Mar 27, 2013 at 21:34
  • 1
    I'm not adding validation, I'm replacing existing validation.
    – Aboodred1
    Mar 27, 2013 at 21:36
  • Oh, is it because we don't want to call user_account_form_validate() (which is index 0 in the #validate array) without changing the values first? I get it now. Thanks
    – Pax
    Mar 27, 2013 at 21:36
  • 1
    exactly, I also changed the text in my answer.
    – Aboodred1
    Mar 27, 2013 at 21:37

Your Answer

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

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