You are here

function user_reference_field_validate in References 7.2

Implements hook_field_validate().

Possible error codes:

  • 'invalid_uid': uid is not valid for the field (not a valid user id, or the

user is not referenceable).

File

user_reference/user_reference.module, line 177
Defines a field type for referencing a user from a node.

Code

function user_reference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {

  // Extract uids to check.
  $ids = array();

  // First check non-numeric uid's to avoid losing time with them.
  foreach ($items as $delta => $item) {
    if (is_array($item) && !empty($item['uid'])) {
      if (is_numeric($item['uid'])) {
        $ids[] = $item['uid'];
      }
      else {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'invalid_uid',
          'message' => t('%name: invalid input.', array(
            '%name' => $instance['label'],
          )),
        );
      }
    }
  }

  // Prevent performance hog if there are no ids to check.
  if ($ids) {
    $options = array(
      'ids' => $ids,
    );
    $refs = user_reference_potential_references($field, $options);
    foreach ($items as $delta => $item) {
      if (is_array($item)) {
        if (!empty($item['uid']) && !isset($refs[$item['uid']])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'invalid_uid',
            'message' => t("%name: this user can't be referenced.", array(
              '%name' => $instance['label'],
            )),
          );
        }
      }
    }
  }
}