I'm using drupal commons and I want to allow user to pick his username on registration.

Currently drupal commons does not allow user to choose his own username but generates one automatically.

What I have done so far through hook_user_register_form_alter :

I changed the #type of name (username machine name) from hidden to textfield. This display the field.

$form['account']['name']['#type'] = 'textfield';

I removed the default value

$form['account']['name']['#value'] = NULL;

Now when I try to add a new user, I get the following error message:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name': INSERT INTO {users} (uid, name, pass, mail, signature, signature_format, created, status, timezone, language, picture, init, data, uuid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( [:db_insert_placeholder_0] => 824 [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => $S$DgHF.OcL6K0hxQ0A4v.O67r4FUp4kpfeuywOrGHvb4zmoEzjpQrW [:db_insert_placeholder_3] => test@test.com [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => filtered_html [:db_insert_placeholder_6] => 1463562222 [:db_insert_placeholder_7] => 1 [:db_insert_placeholder_8] => Europe/Brussels [:db_insert_placeholder_9] => en [:db_insert_placeholder_10] => 0 [:db_insert_placeholder_11] => test@test.com [:db_insert_placeholder_12] => a:2:{s:7:"contact";i:0;s:20:"l10n_client_disabled";b:0;} [:db_insert_placeholder_13] => 267c9864-8fe0-4cc5-81ef-22bd53befcf8 ) in drupal_write_record() (line 7333 of /home/me/websites/customprojects/mysite.local/docroot/includes/common.inc). Backtrace:
PDOStatement->execute(Array) database.inc:2171
DatabaseStatementBase->execute(Array, Array) database.inc:683
DatabaseConnection->query('INSERT INTO {users} (uid, name, pass, mail, signature, signature_format, created, status, timezone, language, picture, init, data, uuid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13)', Array, Array) query.inc:36
InsertQuery_mysql->execute() common.inc:7333
drupal_write_record('users', Object) user.module:582
user_save(Object, Array) user.module:3912
user_register_submit(Array, Array) form.inc:1519
form_execute_handlers('submit', Array, Array) form.inc:903
drupal_process_form('user_register_form', Array, Array) form.inc:385
drupal_build_form('user_register_form', Array) form.inc:130
call_user_func_array('drupal_get_form', Array) menu.inc:527
menu_execute_active_handler() index.php:21

I understand that it is a duplicate problem on the username field but checking in the database there is no username matches the one I have chosen. So don't understand the error.

Also, I'm wondering if the fact of having username can have a side effect ?

1 Answer 1


I removed the default value

Sort of but not really - what you've actually done is set a static value of NULL for that field, which will be carried through to the submission regardless of any user input.

Your best bet to avoid these sort of problems would be to implement the field again from scratch:

$form['account']['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Username'),
  // ...

Note that this won't remove any extra validation that Commons might add, so YMMV and there may be some additional steps to complete your feature.

Just for future reference, to set an initial value for an element that doesn't nuke user input, use #default_value in place of #value.

  • Thanks @Clive, I didn't think to implement it again as for me the field exist then thought of altering the value. code #default_value wasn't set and instead code #value was.
    – funkyD
    May 23, 2016 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.