在我看来,你可以用两种不同的方法来做。让我们创建一个名为
mycustomvalidation.module模块
(记住创建
mycustomvalidation.info文件
文件)。
下面的代码还没有经过测试,所以您可能需要做一些小的调整。顺便说一下,这是Drupal6.x代码。
1) 使用
hook_user()
您需要的是一个自定义模块,其中包含您自己的
http://api.drupal.org/api/function/hook_user/6
.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) 使用
form_alter()
以及自定义验证函数
就我个人而言,我会选择这个选项,因为我觉得它更干净,更“正确”。我们在这里向profile字段添加一个自定义验证函数。
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>