0

I have an user register form alter on my template.php and this is the code:

function mymodule_theme(&$existing, $type, $theme, $path){

  $hooks = array();
  $hooks['user_register_form'] = array(
    'render element'=>'form',
    'title' => 'Registro de candidatas',
    'template'    =>'templates/user-register',
  );
  return $hooks;

}

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

  $form['mail'] = array(
    '#type' => 'textfield',
    '#title_display' => 'invisible',
    '#attributes' => array(
      'placeholder' => t('Escribe tu correo electrónico'),
      'class' => array(
        'mymodule-form-email-input'
      )
    ),
    '#required' => true
  );
  $form['pass'] = array(
    '#type' => 'password',
    '#title_display' => 'invisible',
    '#attributes' => array(
      'placeholder' => t('Escribe tu contraseña'),
      'class' => array(
        'mymodule-form-email-input'
      )
    ),
    '#required' => true
  );
  $form['captcha'] = array(
    '#type' => 'captcha',
    '#captcha_type' => 'recaptcha/reCAPTCHA',
    '#attributes' => array('class' => 'captcha')
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Regístrate ahora')
  );

  $form['#validate'][] = 'mymodule_form_user_register_form_validate';
  $form['#submit'][] = 'mymodule_form_user_register_form_submit';

  return $form;

}

function mymodule_form_user_register_form_validate($form, &$form_state) {

  var_dump($form_state);

  echo 'This is my function';
  drupal_set_message(t('AN ERROR OCURED'));

  if (empty($form_state['values']['mail'])) {
    form_set_error('mail', t('Escribe un correo electrónico'));
  } else {
    if(valid_email_address($form_state['values']['mail'])) {
      form_set_error('mail', t('Email invalido')); 
    }
  }

  if (empty($form_state['values']['pass'])) {
    form_set_error('pass', t('Escribe una contraseña')); 
  }

  /*
  if (!$form_state['values']['sendgift']) {
    form_set_error(NULL, '', TRUE);
    drupal_get_messages();
  }
  */

}

function mymodule_form_user_register_form_submit($form, &$form_state) {

  echo ('This is my custom message');
  /*
  $newuser = array(
    'pass' => $form_state['values']['pass']['pass2'],
    'mail' => $form_state['values']['mail'],
    'init' => $form_state['values']['mail'],
    'status' => 1,
  );         
  $user = user_save('', $newuser);
  $user->password = $pass; // Add plain text password into user account to generate mail tokens.
  _user_mail_notify('register_no_approval_required', $user);

  drupal_set_message('Tu registro ha sido procesado, ahora ve a tu email y activalo!');
  */

}

And I have the callbacks mymodule_form_user_register_form_validate and mymodule_form_user_register_form_submit inside mymodule_form_user_register_form_alter but these functions aren't called when I submit the form. Something wrong here? Please help. Thanks!

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Feb 15 '18 at 14:57
0

The hook hook_form_FORM_ID_alter does not return a variable. The $form variable is passed by reference so it can be modified inside the alter hook.

Remove the return $form; line from your mymodule_form_user_register_form_alter function.

Make sure you clear your cache after code changes to any of those functions.

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.