0

I'm newbie on Drupal and I try to set an validation and submit the custom user registry form but I try and validation not call.

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

  $hooks = array();
   // Make user-register.tpl.php available
  $hooks['user_register_form'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'mymodule') . '/templates',
    'template' => 'user-register',
    'preprocess functions' => array('mymodule_preprocess_user_register_form'),
  );
  return $hooks;

}

function mymodule_preprocess_user_register_form(&$vars) {

  $vars['title'] = 'Registro de candidatas';
  $vars['message_welcome'] = '<p>Ingresar tu correo electrónico para iniciar este proceso. Asegúrate de tener abierto dicho correo para recibir las notificaciones y realizar de manera inmediata las indicaciones que recibas.</p>';

  //Shortens the form variable name for easier access
  $form = $vars['form'];


  $form['account']['mail']['#attributes'] = array(
    'placeholder' => 'Escribe tu correo electrónico',
    'class' => array(
      'mymodule-form-email-input'
    )
  );
  $form['account']['mail']['#title_display'] = 'invisible';
  $form['account']['mail']['#description'] = '';

  $form['account']['pass']['pass1']['#title_display'] = 'invisible';
  $form['account']['pass']['pass1']['#attributes'] = array(
    'placeholder' => 'Escribe una contraseña',
    'class' => array(
      'mymodule-form-email-input'
    )
  );

  $form['account']['pass']['pass2']['#title_display'] = 'invisible';
  $form['account']['pass']['pass2']['#attributes'] = array(
    'placeholder' => 'Vuelve a escribir contraseña',
    'class' => array(
      'mymodule-form-email-input'
    )
  );

  $vars['mail'] = render($form['account']['mail']);
  $vars['pass'] = render($form['account']['pass']);
  $vars['captcha'] = render($form['captcha']);

}

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

  $form['account']['mail']['#field_prefix'] = '';
  $form['#validate'][] = 'mymodule_user_register_form_validate';

}

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

  drupal_set_message(t('AN ERROR OCURED'));

  if ($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 ($form_state['values']['pass']['pass1'] !== '') {
    form_set_error('mail', t('Escribe una contraseña')); 
  }

  if ($form_state['values']['pass']['pass2'] !== '') {
    form_set_error('mail', t('Confirma tu contraseña')); 
  }

  if ($form_state['values']['pass']['pass1'] !== $form_state['values']['pass']['pass2']) {
    form_set_error('mail', t('La contraseñas no coinciden')); 
  }

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

}

Something I have wrong? I look on documentation and I found few details on registration. I read about hook_form_alter() and hook_preprocess_form() but it's confusing to me.

Thanks

0

The most obvious issue with your code is that you are trying to override the entire form structure within a theme preprocess hook. This does not work. Such hooks will be able to alter the output of forms or any other templates by providing additional variables, altering existing (theming) variables, or adding theme wrappers. They will not be able to alter the processing logic of a form.

Instead, you should manipulate your entire form within hook_form_FORM_ID_alter().

The second issue is, that your implementation of hook_form_FORM_ID_alter() is missing the _form_ part between mymodule and the form ID.

Also, consider using the t() function with English texts for all labels and add your desired translations to Drupal I stead of using hard-coded strings.

  • Okay. If I already create another fields that I don't need to show and I really need that an user can register with only email and password and after confirm registry show another fields. Using the hook_form_alter can I custom that fields? – josotoru Feb 15 '18 at 1:04
  • You should be able to do all this within your hook_form_FORM_ID_alter(). (mymodule_form_user_register_form_alter). For reference, have a look to the user module. In the most current version lines 3823ff, or api.drupal.org/api/drupal/modules!user!user.module/function/…. – Mario Steinitz Feb 15 '18 at 1:16
  • I made the for as your suggestion and perfect, but I still have troubles with calling functions. In my code inside mymodule_form_user_register_form_alter() I added the following instructions: $form['#validate'][] = 'mymodule_user_register_form_validate'; $form['#submit'][] = 'mymodule_user_register_form_submit'; Can I have something wrong? Because this functions cannot call. – josotoru Feb 15 '18 at 4:06

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.