You are here

function ref_field_field_widget_validate in (Entity)Reference Field Synchronization 7

FAPI validation of an individual element.

1 string reference to 'ref_field_field_widget_validate'
ref_field_field_widget_form in ./ref_field.module
Implements hook_field_widget_form().

File

./ref_field.module, line 495

Code

function ref_field_field_widget_validate($element, &$form_state) {

  // Save previous references to ensure uniqueness
  static $previous = array();
  $error = TRUE;

  // Get field definition
  $field = field_widget_field($element, $form_state);

  // Get instance definition
  $instance = field_widget_instance($element, $form_state);

  // Extract the IDs
  $matches = ref_field_decode($element['#value']);
  if (!empty($element['#value']) && isset($matches['id'])) {

    // If already referenced, give error
    if (isset($previous[$element['#field_name']]) && in_array($matches['id'], $previous[$element['#field_name']])) {
      form_error($element, t('References must be unique, you already references this entity.'));
      $error = TRUE;
    }
    elseif ($entity = entity_load($matches['entity_type'], array(
      $matches['id'],
    ))) {

      // Set value to the ID
      form_set_value($element, $matches['id'], $form_state);
      $previous[$element['#field_name']][] = $matches['id'];
    }
  }

  //  if (!empty($element['#value']) && !isset($entity)) {
  //    form_error($element, t('You selected an invalid entity'));
  //    $error = TRUE;
  //  }
}