Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active March 19, 2025 07:36
Show Gist options
  • Save vyspiansky/258f11724d818a5be6411f7b31f62cfc to your computer and use it in GitHub Desktop.
Save vyspiansky/258f11724d818a5be6411f7b31f62cfc to your computer and use it in GitHub Desktop.
Change client-side password validation message in Drupal 10

We are going to replace "Make it at least 8 characters" from the user core modules (core/modules/user/user.module) with "Make it at least 12 characters".

/**
 * Implements hook_element_info_alter().
 */
function <your_module>_element_info_alter(array &$types): void
{
    if (isset($types['password_confirm'])) {
        $types['password_confirm']['#process'][] = 'users_form_process_password_confirm';
    }
}

/**
 * Form element process handler for client-side password validation.
 */
function <your_module>_form_process_password_confirm($element): array
{
    if (!empty($element['#attached']['drupalSettings']['password'])) {
        $element['#attached']['drupalSettings']['password']['tooShort'] = t('Make it at least 8 characters');
    }

    return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment