diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index 0bcfad9..3a2b57d 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -407,4 +407,49 @@ public function submitForm(array &$form, FormStateInterface $form_state) { unset($_SESSION['pass_reset_'. $user->id()]); } } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + $name_taken = FALSE; + $mail_taken = FALSE; + + // For new registrations, make sure the username does not conflict with + // an existing user's email address. + $name_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') + ->condition('ufd.mail', db_like($form_state->getValue('name')), 'LIKE') + ) + ->condition('ufd.status', 1) + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + + if ($name_taken) { + $form_state->setErrorByName('name', $this->t('The name @name is already taken.', array('@name' => $form_state->getValue('name')))); + } + + // For new registrations, make sure the email address does not conflict + // with an existing user's username. + $mail_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ) + ->condition('ufd.status', 1) + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + + if ($mail_taken) { + $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $form_state->getValue('mail')))); + } + } + } diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/src/Tests/UserRegistrationTest.php index 78a5f57..927f40f 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/src/Tests/UserRegistrationTest.php @@ -188,7 +188,7 @@ function testRegistrationConflicts() { // Attempt to create a new account using an email that matches an existing // username. $this->drupalPostForm('user/register', $edit, t('Create new account')); - $this->assertText(t('The e-mail address @email is already registered.', array('@email' => $edit['mail'])), "A user cannot be created when their email address matches an existing username."); + $this->assertText(t('The email address @email is already registered.', array('@email' => $edit['mail'])), "A user cannot be created when their email address matches an existing username."); } function testRegistrationDefaultValues() {