UniqueFieldValueValidator does not work when used as a constraint for the Entity ID Field

Created on 12 September 2019, over 5 years ago
Updated 3 September 2024, 4 months ago

Problem/Motivation

The UniqueFieldValueValidator does not work if the field which it is validating is also the field used for the Entity ID.

This is an edge-case and only appears when the Entity Type provides the ability for the user to specify what the ID of the Entity being created should be.

    $entity_id = $entity->id();
    if (isset($entity_id)) {
      $query->condition($id_key, $entity_id, '<>');
    }

    $value_taken = (bool) $query
      ->condition($field_name, $item->value)

In a normal use case where the validator is not being used to check against the Entity ID field, this code will work because it will find all Entities except the current one which have the value set. In other words it will check two different fields, the Entity ID field and the field that it is validating.

However in this edge-case those two conditions will both check for the Entity ID field and so effectively cancel each other out, meaning the query will always return no results, and so the validation will always pass even when it should fail.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Closed: duplicate

Version

11.0 🔥

Component
Entity 

Last updated about 15 hours ago

Created by

🇬🇧United Kingdom AaronMcHale Edinburgh, Scotland

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024