有什么办法允许用户名中的特殊字符,如“#”和“〜”?
有什么办法允许用户名中的特殊字符,如“#”和“〜”?
Answers:
您必须重写defualt user_validate_name($ name):
Verify the syntax of the given name.
为此,请在自定义模块中定义用户名验证代码
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register') {
$form['#validate'] = array();
$form['#validate'][] = 'registration_username_validate';
}
}
在registration_username_validate函数内部,执行您的逻辑以允许使用特殊字符。
注意由允许在用户名中使用特殊字符引起的安全性问题,以及未经过消毒的用户名可能导致的主题显示问题。
user_account_form_validate
函数,该函数验证的不仅仅是用户名。替换这个可能允许重复的用户名! 我添加了一个解决这些问题的答案。
不幸的是,没有直接的方法可以做到这一点。默认情况下,user_register_form
并user_profile_form
已将其user_account_form_validate
设置为中的第一个验证器$form['#validate']
。user_account_form_validate()
检查并清理帐户的名称,电子邮件和签名。作为检查名称的一部分,它会呼叫user_validate_name()
。这是我们要覆盖的功能,而不是user_account_form_validate
。
人们希望有一个有用的钩子来替代它,但是but。如果我既不关心电子邮件和签名的验证,也不想检查名称是否重复,可以user_account_form_validate
从中
删除$form['#validate']
。但这不好。取而代之的是,我添加了一个附加的验证器,该验证器撤消了工作user_validate_name()
并重做了所有内容而无需特殊字符检查。
<?php
function MODULENAME_form_user_register_form_alter(
array &$form, array &$form_state, $form_id)
{
MODULENAME_add_name_validator($form);
}
function MODULENAME_form_user_profile_form_alter(
array &$form, array &$form_state, $form_id)
{
MODULENAME_add_name_validator($form);
}
function MODULENAME_add_name_validator(array &$form)
{
$validate =& $form['#validate'];
# Since `validate_name()` clears out any errors for the "name" field, we
# want to put it right after the validator we want to partially override.
$acct_validate_index = array_search('user_account_form_validate', $validate);
array_splice($validate, ($acct_validate_index + 1), 0,
['MODULENAME_validate_name']
);
}
function MODULENAME_validate_name(array $form, array &$form_state)
{
# There is no blessed way of overriding the call to `user_validate_name()` in
# `user_account_form_validate()`.
$errors =& drupal_static('form_set_error', []);
# Yes, this gets the errors. `form_get_error()` uses this method so... yeah.
if (!isset($errors['name']))
# `user_validate_name()` is a superset of what is checked here. If the name
# passed that validation, no need to rerun things.
return;
# `form_set_error()` also calls `drupal_set_message()` if it finds an
# error.
$msg_index = array_search($errors['name'], $_SESSION['messages']['error']);
if ($msg_index !== false) {
unset($_SESSION['messages']['error'][$msg_index]);
if (empty($_SESSION['messages']['error']))
unset($_SESSION['messages']['error']);
}
unset($errors['name']);
$name = isset($form_state['values']['name'])
? $form_state['values']['name'] : null;
# These checks are taken from `user_validate_name()`, simply excluding the
# for characters we don't mind being in the names.
if (!$name)
$error = t('You must enter a username.');
else if (substr($name, 0, 1) == ' ')
$error = t('The username cannot begin with a space.');
else if (substr($name, -1) == ' ')
$error = t('The username cannot end with a space.');
else if (strpos($name, ' ') !== FALSE)
$error = t('The username cannot contain multiple spaces in a row.');
else if (preg_match('/[\x{80}-\x{A0}' . // Non-printable ISO-8859-1 + NBSP
'\x{AD}' . // Soft-hyphen
'\x{2000}-\x{200F}' . // Various space characters
'\x{2028}-\x{202F}' . // Bidirectional text overrides
'\x{205F}-\x{206F}' . // Various text hinting characters
'\x{FEFF}' . // Byte order mark
'\x{FF01}-\x{FF60}' . // Full-width latin
'\x{FFF9}-\x{FFFD}' . // Replacement characters
'\x{0}-\x{1F}]/u', // NULL byte and control characters
$name))
$error = t('The username contains an illegal character.');
else if (drupal_strlen($name) > USERNAME_MAX_LENGTH)
$error = t('The username %name is too long: it must be %max characters '
.'or less.'
,['%name' => $name, '%max' => USERNAME_MAX_LENGTH]);
if (isset($error))
form_set_error('name', $error);
}
仍然存在特殊字符检查,但是它仅检查不可见或特殊用途的字符。
$form['#validate'] = array();
将破坏任何现有的验证处理程序,包括可能由其他contrib / custom模块设置的那些处理程序。最好有选择地取消设置覆盖的验证功能。