Quantcast
Viewing all articles
Browse latest Browse all 294808

UniqueFieldValueValidator works only with single value fields

The \Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator works with only single value field and therefore it cannot be used with fields that have multiple values, like string lists.

On line #34 the condition is:

$value_taken = (bool) \Drupal::entityQuery($entity_type_id)
      // The id could be NULL, so we cast it to 0 in that case.
      ->condition($id_key, (int) $items->getEntity()->id(), '<>')
      ->condition($field_name, db_like($items->first()->value), 'LIKE')
      ->range(0, 1)
      ->count()
      ->execute();

instead it should be:

$values = array();
foreach ($items->getIterator() AS $item) {
  // The original code uses $item->value and although I prefer $item->getValue()
  // we don't know what the key for the field will be so using 'value' is ok.
  $values[] = $item->value;
}

$value_taken = (bool) \Drupal::entityQuery($entity_type_id)
      // The id could be NULL, so we cast it to 0 in that case.
      ->condition($id_key, (int) $items->getEntity()->id(), '<>')
      ->condition($field_name, $values, 'IN')
     // Or ->condition($field_name, ':values[]', 'IN', array(':values[]' => $values))
     // I'm not sure what the current approach is.
      ->range(0, 1)
      ->count()
      ->execute();

Viewing all articles
Browse latest Browse all 294808

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>