Problem/Motivation

UniqueFieldValuesConstraint only makes case insensitive validation.
This was previously because the comparison was done via SQL query which is typically case insensitive.
Now however, it works via PHP

Steps to reproduce

Proposed resolution

Add a configuration option to the constraint to support case insensitivity

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3419816

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Shamrockonov created an issue. See original summary.

larowlan’s picture

Assigned: Unassigned » larowlan
larowlan’s picture

Title: Able to create user accounts with the same email address » Able to create user accounts with the same email address or username
Version: 10.2.x-dev » 11.x-dev
Assigned: larowlan » Unassigned
Status: Active » Needs review

Was expecting this would also impact username, as it also uses that constraint - but we have a unique key on name for the users table, so instead if causes a fatal (SQL) exception - so still an issue.

Added tests and fix for both cases.

divya.sejekan’s picture

Assigned: Unassigned » divya.sejekan
Paras Awasthi’s picture

I am new here, can I work on this issue?

divya.sejekan’s picture

I verified for the use cases given above. The fix is working - Error message are displayed 'The email address EMAIL@TEST.COM is already taken.'

Testing Steps :
1. Creating user with Email@test.com
2. Verify when used different cases such as eMail@test.com , EMAIL@test.com - error message must be displayed
3. Verify when used same case Email@test.com with different usernames - - error message must be displayed

Can be moved to RTBC++

Keeping this in need review for further reviews.

divya.sejekan’s picture

Assigned: divya.sejekan » Unassigned

smustgrave’s picture

Status: Needs review » Needs work

Opened an MR and appears to have test failures.

longwave’s picture

Added a question to the MR.

larowlan’s picture

Title: Able to create user accounts with the same email address or username » Add support for case insentive validation to UniqueFieldValuesConstraint
Category: Bug report » Feature request
larowlan’s picture

Title: Add support for case insentive validation to UniqueFieldValuesConstraint » Add support for case sentive validation to UniqueFieldValuesConstraint
Issue summary: View changes
larowlan’s picture

Title: Add support for case sentive validation to UniqueFieldValuesConstraint » Add support for case sensitive validation to UniqueFieldValuesConstraint
caesius’s picture

cilefen’s picture

Status: Needs work » Postponed (maintainer needs more info)

Is this fixed in total by the security release?

jamsilver’s picture

A comment here that may save others time.

Today, I added the UniqueField constraint onto a base field on a custom entity type, setting caseSensitive to TRUE, and found that it did not work. I created a new entity and entered a value for this field that matched an existing value, except for a change in the case of a single letter (e.g. "bar" vs "Bar"). This validator rejected the new entity. but I would have expected it to accept it.

As it happens, my field is a string type, and I was able to fix this by setting the case_sensitive field option to force the SQL field storage to store it in a format that always behaves case-sensitively in queries. All in all, this is the sort of base field definition I now have:

    $fields['foo'] = BaseFieldDefinition::create('string')
      ->setTranslatable(FALSE)
      ->setLabel(t('Foo'))
      ->setRequired(TRUE)
      ->addConstraint('UniqueField', ['caseSensitive' => TRUE])
      ->setSetting('case_sensitive', TRUE)
      ->setSetting('max_length', 255);

It may be relevant that I'm using MariaDB (version 10.4) rather than MySQL.

On balance this feels to me like unexpected behaviour in the UniqueField Constraint that should be improved. There's nothing in the documentation of the caseSensitive option that indicates extra steps are necessary in the SQL storage to make it work.

The UniqueFieldValueValidator uses an entity query, and entity query has supported case-sensitive comparisons since #2068655: Entity fields do not support case sensitive queries. Perhaps a better implementation here would be to keep the OR condition when caseSensitive, but use an operator like LIKE BINARY?

smustgrave’s picture

Wanted to bump this 1 more time if still relevant.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.