Drupal 7中用户注册表单中的唯一字段

aay*_*tha 1 forms drupal user-registration drupal-7

我在drupal 7工作.我需要一个用户注册表单中的字段,它必须是唯一的.它是存储用户ID的整数字段.我一直在寻找几个小时没有任何发展.有人可以指导我完成这个吗?

谢谢.

mmi*_*les 5

您可以从admin/config/people/account/fields(配置 - >人员 - >帐户设置)向用户实体类型添加自定义"员工ID"字段.您可以添加新的整数字段,并将其标记为在注册表单中显示和/或必需.

要检查字段值是否为unqiue,您需要使用自定义模块.在自定义模块中,使用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');
        }
    }
Run Code Online (Sandbox Code Playgroud)