2009-11-02 56 views

回答

2

我不知道現有的模塊,允許添加自定義驗證的,但它是非常容易實現這一點使用的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,就會重新顯示註冊表單,並在郵件字段中顯示錯誤消息。

如果你想修改自己的帳戶也發生了現有用戶的驗證,你可以從第一跌落

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

一部分,如果第 - 代碼將然後爲每個用戶編輯表單提交運行,而不是隻是註冊。

1

如果您使用LDAP服務器進行身份驗證,則有LDAP module。檢查出。

對於使用其他Web服務進行身份驗證,您必須編寫一個模塊並實現hook_user,特別是'登錄'情況。如果在登錄時用戶的憑證與您的Web服務中的憑證不匹配,則可以將其註銷並顯示消息。

+0

對LDAP模塊和'hook_user'建議+1,但我不同意'檢查登錄'部分。該檢查應在*登錄/註冊之前發生,使用「驗證」操作。這樣用戶不會首先登錄,因此不需要再次登錄 - 請參閱我的示例單獨的答案。 – 2009-11-02 20:13:50

+0

有趣......我從來不會從api.drupal.org上的「驗證」文檔中發現這一點。似乎需要重寫才能更清晰一些。感謝您的更正。 – theunraveler 2009-11-03 01:33:11

+0

@theunraveler:爲了防止誤解 - 在'validate'操作期間使用'form_set_error()'只會阻止* registration *之後的登錄,因爲它是要驗證的註冊表單(和帳戶編輯表單,如果有的話不檢查)。它不會阻止現有用戶的*登錄*,在這種情況下,您的方法將成爲一種選擇(可選擇向登錄表單添加自定義驗證回調)。 – 2009-11-03 08:04:52