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.

Comments

Vadym.Tseiko created an issue. See original summary.

vadym.tseiko’s picture

StatusFileSize
new674 bytes

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

darvanen’s picture

Title: Email validation is also triggering on not required fields » Email validation is always triggering on optional fields
Version: 1.1.4 » 1.0.x-dev
Priority: Minor » Normal
Status: Active » Needs work
Issue tags: +Needs tests

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.

darvanen’s picture

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?

darvanen’s picture

StatusFileSize
new849 bytes

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

sumit-k’s picture

StatusFileSize
new903 bytes

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.

sumit-k’s picture

Status: Needs work » Needs review
vadym.tseiko’s picture

Hi @darvanen, the user's email is optional because of the strange flow with content management 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.

darvanen’s picture

@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.

darvanen’s picture

Status: Needs review » Needs work

Oh and this is NW for tests.

darvanen’s picture

Status: Needs work » Needs review
StatusFileSize
new951 bytes

Here we go, this should fail.

Status: Needs review » Needs work

The last submitted patch, 11: 3365661-11-test-only.patch, failed testing. View results

  • darvanen authored 82c29807 on 1.0.x
    Issue #3365661 by darvanen, Vadym.Tseiko, sumit-k: Email validation is...
darvanen’s picture

Status: Needs work » Fixed
Issue tags: -Needs tests

Committed.

Status: Fixed » Closed (fixed)

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