Email validation is always triggering on optional fields

Created on 8 June 2023, about 1 year ago
Updated 14 June 2023, about 1 year ago

Problem/Motivation

Hello on my project D9 core version 9.5.9, we have optional email fields. Still, there was a problem when some people left emails like test@test.test so we decided to install the Advanced Email Validation module in order to check on valid email domains. But this unfortunately breaks the logic of our user creation process. We create a user with an optional email, but when we try to create a user without an email(field is not required) we get an error message about not valid email format when the field is left empty.

Steps to reproduce

Install clean Drupal 9. Install the Advanced Email Validation module and add configurations on /admin/config/people/advanced-email-validation. Go to admin->people->create. Add the user but leave the email field empty. And u will get an error like showed on the screen.

Proposed resolution

I'm not very experienced with Drupal, but I've been added easy fix for this problem with my patch, will add it here.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇦Ukraine Vadym.Tseiko

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

Comments & Activities

  • Issue created by @Vadym.Tseiko
  • 🇺🇦Ukraine Vadym.Tseiko

    I solved this issue with a small local patch, if someone will get in the same spot you can use it.

  • Status changed to Needs work about 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    This is great, thanks. You're right, we shouldn't be attempting to validate empty email addresses.

    A unit test on the affected method would be sufficient for this I think and then I'll happily commit it.

  • 🇦🇺Australia darvanen Sydney, Australia

    Ok so looking further into this, the validator's job is to validate a string as a valid email or not. So the response it is providing is correct. Where we need to fix this is in the user Constraint which triggers the validation - it should not be testing an empty result.

    The thing is though, optional email addresses for Drupal users causes problems - like not being able to use the 'forgot password' system. What are you doing to make the email address optional?

  • 🇦🇺Australia darvanen Sydney, Australia

    This still requires testing, but is a better solution to the issue, per #4.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    7 pass
  • 🇮🇳India sumit-k

    I have verified that the shared patch #5 is functioning correctly. However, it would be beneficial to enhance readability and adhere to Drupal coding standards by using empty() instead of negating the assignment within the if condition.

    To address this, I have prepared an updated patch that implements the recommended changes. Kindly find the attached patch file.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    7 pass
  • 🇺🇦Ukraine Vadym.Tseiko

    Hi @darvanen, the user's email is optional because most of the flow with content management is inside the project. I'm agreed with you that better to use the email address and this is not the best flow, but I've been asked, to fix a problem of creating users with no email. And yes first I've added changes to the user Constraint too. But then I thought that maybe there can be other fields that have optional email(like forms with not required field email, leave contact information or something so I added this check to the advanced email validator) So if field is required it would not let to leave field be empty then. But I guess your approach is better. Anyway, thanks for your response I will replace my patch to patch #6.

  • 🇦🇺Australia darvanen Sydney, Australia

    @sumit-k can you reference where in the coding standards this is mentioned please? And did you test it out with a user that has an empty email address? Please be specific about what you did to manually test the patch.

    @Vadym.Tseiko fair enough, requirements are strange beasts from time to time, I imagine you used a hook of some kind to alter the User entity and the login form?

    For other optional fields to use this module they will need a special implementation - which would be the place to check whether the string is empty first.

    I think what this needs is a test to ensure *our* constraint doesn't fail even if the required field one does.

  • Status changed to Needs work about 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Oh and this is NW for tests.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    1 fail
  • 🇦🇺Australia darvanen Sydney, Australia

    Here we go.

    • darvanen authored 82c29807 on 1.0.x
      Issue #3365661 by darvanen, Vadym.Tseiko, sumit-k: Email validation is...
  • Status changed to Fixed about 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Committed.

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

Production build 0.69.0 2024