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