Add validation to email field in custom entity form

Created on 18 July 2023, over 1 year ago
Updated 6 August 2023, over 1 year ago

Firstly, thanks for a great module! It's making a big difference in reducing the number of incorrect/spam user registrations on our site.

Within our site, we have a custom entity form and one of the fields on the form requests the user's email address:

$form['booking_email_address'] = [
      '#type' => 'email',
      '#title' => $this->t('Email Address'),
      '#maxlength' => 64,
      '#size' => 64,
      '#required' => TRUE,                
    ];

I'm just wondering if there might be any way to manipulate that field/form to be able to use the validation tools from this module against this email field?

💬 Support request
Status

Fixed

Version

1.1

Component

Code

Created by

🇬🇧United Kingdom ttesteve

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ttesteve
  • Status changed to Needs review over 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    There sure is, two of them ;)

    Since you're working with a custom entity I would advise the first method - add a constraint to the field definition:

    $fields['booking_email_address']->addConstraint('AdvancedEmailValidation');

    Or if you're adjusting an existing entity you can see how the module does that in \advanced_email_validation_entity_base_field_info_alter.

    If you want to adjust the validation settings then I would clone or extend the existing Validation and Validator to make your own that inject the desired settings into \Drupal\advanced_email_validation\AdvancedEmailValidator::validate as the second argument.

    ...

    If you want to do it in the form you'll want to add the call inside the validateForm method.

    You can borrow and adjust code from \Drupal\advanced_email_validation\Plugin\Validation\Constraint\AdvancedEmailValidationValidator.

    Again if you want to override the global settings you can pass new settings in to \Drupal\advanced_email_validation\AdvancedEmailValidator::validate as the second argument.

    ...

    Do let me know how you go. If you get stuck you can find me on Drupal Slack as @darvanen, usually in #australia-nz and mainly during AEST business hours.

  • 🇬🇧United Kingdom ttesteve

    Hi @darvanen!

    Thanks so much for the quick reply on this! I'll take a look at those notes and see which method is best to apply to my own entity. Really appreciate the offer of the extra support too - I'll shout up if I get stuck!

    Cheers,
    Steve

  • 🇦🇺Australia darvanen Sydney, Australia

    @ttesteve how did you go? Can we mark this issue as fixed?

  • Status changed to Fixed over 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Just doing some tidying. Since there's no response here I'm going to assume this is sorted and close it. Please feel free to reopen if you need more support.

  • 🇦🇺Australia darvanen Sydney, Australia

    forgot to credit myself, did the main response on work time.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024