0

I want to customise registration form action in my drupal8 site.I want to check email address is already existing.I have tried with different queries.But not worked.Can anyone to help me to solve this issue?

3
  • Can you post what you've done? Are you modifying the existing form, or trying to create a new registration form from scratch? As Alex mentioned, the default registration form already handles this for you.
    – Andy
    Jul 30, 2018 at 8:31
  • @salu Let us know what you're trying and what you have done till now to achieve this.
    – Ajay Reddy
    Jul 30, 2018 at 9:22
  • I want to validate default registration form in drupal8.I added a custom ajax handler to validating the form.Because: 1) A user registering with already existing email id. 2)Then no validation message such as "This email id is already exsiting" will be shown to the user.And user can submit the form. 3)Then user will get an email like "Registration is unsuccesful because emai id is already existing". What i did now is :
    – salu
    Jul 30, 2018 at 9:26

2 Answers 2

0

Use this code to check if there is a user with provided email.

$account = user_load_by_mail($email);

if ($account) {
  // Do something.
  $form_state->setError(....
}

Besides, as I know in standard registration form there is already such validation.

2
  • Thanks for your reply.I have tried this in my custom module.But it is not working.
    – salu
    Jul 30, 2018 at 7:59
  • Yes..but i want to customise the things as well.When a user register with email id,whether email id exist or not,user will get the email regarding duplication of email id.
    – salu
    Jul 30, 2018 at 8:45
0

As pointed out in the comments the email uniqueness is already checked by default.

See core/modules/user/src/Entity/User.php

$fields['mail'] = BaseFieldDefinition::create('email')
      ->setLabel(t('Email'))
      ->setDescription(t('The email of this user.'))
      ->setDefaultValue('')
      ->addConstraint('UserMailUnique')
      ->addConstraint('UserMailRequired')
      ->addConstraint('ProtectedUserField');

However, you should be able to create your own validation constraint.

First, implement hook_entity_bundle_field_info_alter()

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {

  if ($entity_type->id() === 'user') {
    $fields['email']->addConstraint('MyConstraint');
  }
}

Then your classes

<?php

namespace Drupal\mymodule\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
 * My Constraint.
 *
 * @Constraint(
 *   id = "MyConstraint",
 *   label = @Translation("Check My Constraint"),
 * )
 */
class MyConstraint extends Constraint {

  public $message = 'My message @email';

}

And

    <?php
namespace Drupal\mymodule\Plugin\Validation\Constraint;

    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Validator\ConstraintValidator;

    /**
     * MyConstraintsValidator class.
     */
    class MyConstraintsValidator extends ConstraintValidator {

      /**
       * Checks if the passed value is valid.
       */
      public function validate($value, Constraint $constraint) {

        $this->context->addViolation($constraint->message, ['@email' => 'foo']); 
      }    
    }

I have not tested the code above but should get you most of the way there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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