You are here

function rolereference_autocomplete_validate in Role Reference 6

Validate an autocomplete element.

Remove the wrapper layer and set the right element's value. This will move the nested value at 'field-name-0-rid-rid' back to its original location, 'field-name-0-rid'.

1 string reference to 'rolereference_autocomplete_validate'
rolereference_autocomplete_process in ./rolereference.module
Process an individual element.

File

./rolereference.module, line 600
Defines a field type for referencing a role. Based almost entirely on nodereference and userreference modules.

Code

function rolereference_autocomplete_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $field = content_fields($field_name, $type_name);
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
  $value = $element['#value'][$field_key];
  $rid = NULL;
  if (!empty($value)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*rid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // Explicit [rid:n].
      list(, $name, $rid) = $matches;
      if (!empty($name) && ($r = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid))) && $name != $r) {
        form_error($element[$field_key], t('%name: name mismatch. Please check your selection.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
    }
    else {

      // No explicit rid.
      $reference = _rolereference_potential_references($field, $value);
      if (empty($reference)) {
        form_error($element[$field_key], t('%name: found no valid role with that name.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
      else {

        // TODO:
        // the best thing would be to present the user with an additional form,
        // allowing the user to choose between valid candidates with the same title
        // ATM, we pick the first matching candidate...
        $rid = key($reference);
      }
    }
  }
  form_set_value($element, $rid, $form_state);
}