3

I'm using Omega's sub-theme 'creativpink' and ajax_register module.

When user enters wrong user name or wrong password I want show *'The password entered is wrong. Please retry.' instead of Drupal's default error 'Sorry, unrecognized username or password. Have you forgotten your password?'.

I've tried with following code in my sub-theme's template.php file :

function _custom_validation(&$form, &$form_state){
  $query = db_select('users', 'u');
  $query->fields('u', array('name'));
  $query->condition('u.name', $form_state['values']['name'], '=');
  $result = $query->execute();
  $name = $result->fetchField();
  if(empty($name)){
    form_set_error('user_name', t('The password entered is wrong. Please retry.'));
  }
}

After saving file I entered wrong user name and password. The error message showed both default text and my custom text.

  • Sorry, unrecognized username or password. Have you forgotten your password?
  • The password entered is wrong. Please retry.

But I want to show only my custom error message. How to do this?

1

4 Answers 4

1

You can use function form_get_errors and function form_clear_error

function _custom_validation($form,$form_state){
  $errors = form_get_errors();
  if(!empty($errors)){
    $string_error = $errors['name'];
    if (strpos($string_error, 'unrecognized username or password') !== FALSE) {
      form_clear_error();
      form_set_error('name','The password entered is wrong. Please retry.');
    }
  }
}
0

The error messages thrown by Drupal core user module are defined in

function user_login_final_validate()

This can be overridden in any of your own module/theme by calling the form_alter function and replace user_login_final_validate() with your own validator.

Example below:

/**
 * Implements hook_form_alter().
 */
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_login':
    case 'user_login_block':
      $user_login_final_validate_index = array_search('user_login_final_validate', $form['#validate']);
      if ($user_login_final_validate_index >= 0) {
        $form['#validate'][$user_login_final_validate_index] = 'your_custom_final_validate';
  }
    break;
  }
}

function your_custom_final_validate($form, &$form_state) {
  /* Copy the code from user.module and change to your own message */
}
0

You can use one of the two methods outlined here: http://www.bluespark.com/blog/youre-doing-it-wrong-dont-hack-drupal-core-change-text

0

You could use the String Overrides module. According to the project page, the module "Provides a quick and easy way to replace any text on the site."

I've used it before. It is quite easy to configure and works like a charm.

https://www.drupal.org/project/stringoverrides

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.