在 Drupal 中使用外部身份存储库验证注册数据 [英] Validating Registration Data with an External Identity Repository in Drupal

查看:18
本文介绍了在 Drupal 中使用外部身份存储库验证注册数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的用户尝试注册时,我想通过检查外部身份存储库(例如调用网络服务或查找目录服务器)来确保他的信息有效.

When my user tries to register, I'd like to make sure his information is valid, by checking an external identity repository (e.g. call a web service or look up a directory server).

是否可以使用任何现有模块?如果没有,开发此功能的最佳方式是什么?

Is that possible with any existing module? If not, what would be the best way to develop this functionality?

推荐答案

我不知道允许添加自定义验证的现有模块,但使用 hook_user():

I'm not aware of an existing module allowing the addition of custom validation, but it is fairly easy to implement this using the 'validate' action of hook_user():

function yourModule_user($op, &$edit, &$account, $category = NULL) {
  // Are we in the validation phase of a new user registration?
  if ('validate' == $op && 'user_register' == $edit['form_id'] && 'account' == $category) {
    // Yes, do custom validation...
    // NOTE: Just an example to validate by email.
    // Check the other elements in $edit array (e.g. 'name') for more options
    $mail_is_valid = yourModule_custom_mail_validation($edit['mail']);
    // Is the mail address OK?
    if (!$mail_is_valid) {
      // No, set error on mail form field
      form_set_error('mail', t('your custom error message'));
    }
  }
}

只要yourModule_custom_mail_validation() 不返回TRUE,就会停止注册过程并重新显示带有错误消息的注册表单.

This would stop the registration process and redisplay the registration form with the error message on the mail field as long as yourModule_custom_mail_validation() does not return TRUE.

如果您还希望对编辑其帐户的现有用户进行验证,您可以删除

If you want the validation to happen for existing users editing their account also, you could drop the

 && 'user_register' == $edit['form_id']

第一个 if 子句的一部分 - 代码将针对每个用户编辑表单提交运行,而不仅仅是在注册时运行.

part from the first if clause - the code would then run for every user edit form submission, not just on registration.

这篇关于在 Drupal 中使用外部身份存储库验证注册数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆