[英]Unique field in user registration form in Drupal 7
我在drupal 7中工作。我需要用户注册表单中的一个字段,该字段必须唯一。 它是一个整数字段,用于存储用户的ID。 我已经搜索了几个小时,没有任何进展。 有人可以指导我吗?
谢谢。
您可以从admin / config / people / account / fields(配置-> people->帐户设置)向用户实体类型添加自定义“ staff id”字段。 您可以添加一个新的整数字段,并将其标记为在注册表中显示,并且/或者是必需的。
要检查字段值是否正确,您将需要使用自定义模块。 在您的自定义模块中,使用form_id_form_alter挂钩将自定义验证添加到注册表单。 然后,在验证过程中,您可以检查该值在数据库中尚不存在,并返回表单错误。
自定义模块的示例:
<?php
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id){
$form['#validate'][] = 'mymodule_user_registration_validate';
}
function mymodule_user_registration_validate(&$form,&$form_state){
$staff_id = $form_state['values']['staff_id_field'];
$check_unique_query = 'SELECT field_staff_id_value FROM {field_data_staff_id} WHERE field_staff_id_value = :staff_id LIMIT 1';
//if a result is returned, that means the $staff_id exists
if (db_query($check_unique_query,array(':staff_id'=>$staff_id))->fetchField()){
form_set_error('staff_id_field','This Staff ID is already in use');
}
}
您是否尝试过“ 唯一字段”模块 ? 相当确定它可以满足您的需求。
现场验证起到了https://drupal.org/project/field_validation的作用 。 您甚至可以在注册表中为每个字段设置任何规则
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.