I am working in drupal 7. I need a field in the user registration form which needs to be unique. It is an integer field which stores the ID of the user. I have been searching around for a few hours now without any development. Can someone guide me through this?

Thank You.

有帮助吗?

解决方案

You can add a custom "staff id" field to the user entity type from admin/config/people/account/fields (configuration->people->account settings). You can add a new integer field, and mark it to display in the registration form, and/or required.

To check that the field value is unqiue you will need to use a custom module. In your custom module use the form_id_form_alter hook to add a custom validation to the registration form. Then during validation you can check that the value does not already exist in the database and return a form error.

Example of the custom module:

<?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');
        }
    }

其他提示

Have you tried the Unique field module? Pretty sure it does what you need here.

Field validation does the trick https://drupal.org/project/field_validation. You can set any rules per field even on the registration form

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top