首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改密码/确认密码密码提示

更改密码/确认密码密码提示
EN

Drupal用户
提问于 2018-05-11 02:51:39
回答 2查看 1.3K关注 0票数 2

如何更改password_confirm表单元素的密码提示?

这一条具体如下:

代码语言:javascript
复制
Recommendations to make your password stronger: 
•Make it at least 12 characters 
•Add lowercase letters
•Add uppercase letters 
•Add numbers 
•Add punctuation

变成像:

代码语言:javascript
复制
Password should have: 
• Minimum of 12 characters 
• At least one lowercase letter
• At least one uppercase letter
• At least one number
• At least one special character
• No three consecutive same characters

我尝试使用hook_element_info_alter()钩子,它调用一个方法my_module_form_process_password_confirm()并遵循这个模板(https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_表格_流程_密码_确认/8.5.x)。

但它似乎不起作用。

代码语言:javascript
复制
/**
 * Implements hook_element_info_alter().
 */
function client_element_info_alter(array &$types) {
  if (isset($types['password_confirm'])) {
    $types['password_confirm']['#process'][] = 'client_form_process_password_confirm';
  }
}

function client_form_process_password_confirm($element){
  if (($key = array_search('user_form_process_password_confirm', $element['#process'])) !== false) {
    unset($element['#process'][$key]);
  }
  $element['#process'] = array_values($element['#process']);

  $password_settings = [
    'confirmTitle' => t('Passwords match DOES IT:'),
    'confirmSuccess' => t('yes'),
    'confirmFailure' => t('no'),
    'showStrengthIndicator' => FALSE,
  ];

  $password_settings['showStrengthIndicator'] = TRUE;
  $password_settings += [
    'strengthTitle' => t('Password strength:'),
    'hasWeaknesses' => t('Password should have:'),
    'tooShort' => t('Minimum of 12 characters'),
    'addLowerCase' => t('At least one lowercase letter'),
    'addUpperCase' => t('At least one uppercase letter'),
    'addNumbers' => t('At least one number'),
    'addPunctuation' => t('At least one special character'),
    'sameAsUsername' => t('Make it different from your username'),
    'weak' => t('Weak'),
    'fair' => t('Fair'),
    'good' => t('Good'),
    'strong' => t('Strong'),
    'username' => \Drupal::currentUser()->getUsername(),
  ];

  $element['#attached']['library'][] = 'user/drupal.user';
  $element['#attached']['drupalSettings']['password'] = $password_settings;

  return $element;
}
EN

回答 2

Drupal用户

发布于 2018-05-17 09:18:49

因此,我设法修补了一些东西来工作,我分享它,以防其他人需要它或什么东西。当然,我不知道这是最优的,或遵循的标准,甚至不应该做的第一,但请随时添加您的投入。它在my_module.module文件中,在by旁边。哦,别介意这篇文章,只是在试一下这个和所有的爵士音乐。

下面是代码,不需要再多说了:

代码语言:javascript
复制
/**
 * Implements hook_form_alter().
 */
function _form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === ) {
    // Note that this isn't actually a hook
    $form['#after_build'][] = '_confirm_password_after_build';
  }
}

function client_confirm_password_after_build($element) {
  $drupal_settings = $element['pass']['#attached']['drupalSettings']['password'];

  $drupal_settings = [
    'confirmTitle'          => t('Do the passwords match?'),
    'confirmSuccess'        => t('Yeah'),
    'confirmFailure'        => t('Nope'),
    'showStrengthIndicator' => TRUE,
    'hasWeaknesses'         => t('Password Suggestions:'),
    'tooShort'              => t('Make it at least 50 characters'),
    'addLowerCase'          => t('Add 7 lowercase letters'),
    'addUpperCase'          => t('Add 8 uppercase letters'),
    'addNumbers'            => t('Add 8 numbers'),
    'addPunctuation'        => t('Add 10 punctuations'),
    'sameAsUsername'        => t(''),
    'weak' => t('Forgettable'),
    'fair' => t('Meh'),
    'good' => t('Almost'),
    'strong' => t('You got it'),
    'username' => '',
  ];

  $element['pass']['#attached']['drupalSettings']['password'] = $drupal_settings;
  return $element;
}
票数 4
EN

Drupal用户

发布于 2018-05-11 06:59:36

您可以使用在这里输入链接描述password_policy

如果您想知道为什么您的代码不起作用,那么您应该将代码包括在您的问题中。

票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/261513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档