参与Drupal注册并根据业务逻辑验证用户信息 [英] Hook into Drupal registration and validate user info against business logic
问题描述
我想加入注册模块。我已经有一个使用我的旧网站的50000个用户的数据库。现在,我要迁移到Drupal。
I want to hook into the registration module. I already have a database of 50000 users who use my old website. Now I am migrating to Drupal.
我还没有将条目迁移到Drupal数据库。我将检查我的旧数据库。
I still haven't migrated the entries to drupal database. I will be checking against my old database.
当用户尝试在Drupal中注册时,我需要检查他提供的用户名是否已经存在于该50000列表中(并且还在不断增加)条目。如果存在,我需要取消注册,并显示一条错误消息,提示用户名存在。.
When a user tries to register in Drupal, I need to check whether the username he gave is already present in that list of 50000 (and growing) entries. If it exists, I need to cancel the registration showing an error msg saying username exists..
我应该使用哪个钩子?如果我的代码认为验证失败,那么如何告诉drupal显示错误消息?
Which hook should I use? If my code figures that the validation failed, How can I tell drupal to display an error msg?
编辑:我钩入了hook_user并检查了验证操作。我能够验证并分配错误消息。但这一切都在发生。我只想验证新的帐户创建表单。我该怎么做?
谢谢。
推荐答案
您应该使用 为注册表单注册其他验证回调函数。 hook_form_FORM_ID_alter()
,有点像这样:
You should register an additional validation callback function for the registration form using hook_form_FORM_ID_alter()
, somewhat like so:
// Alter the registration form
function yourModuleName_form_user_register_alter(&$form, &$form_state) {
// Add your own function to the array of validation callbacks
$form['#validate'][] = 'yourModuleName_user_register_validate';
}
// Perform your own validation
function yourModuleName_user_register_validate($form, &$form_state) {
// Extract the submitted name
$name = $form_state['values']['name'];
// Check it according to your own logic
$is_valid_name = your_check_for_valid_name();
// File error, when not valid
if (!$is_valid) {
form_set_error('name', t('Name already taken, please choose a different one'));
}
}
这篇关于参与Drupal注册并根据业务逻辑验证用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!