You are here

function petreference_autocomplete_validate in Previewable email templates 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-nid-nid' back to its original location, 'field-name-0-nid'.

1 string reference to 'petreference_autocomplete_validate'
petreference_autocomplete_process in modules/petreference/petreference.module
Process an individual element.

File

modules/petreference/petreference.module, line 657
Defines a field type for referencing pet template to a node.

Code

function petreference_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];
  $pid = NULL;
  if (!empty($value)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*pid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // Explicit [nid:n].
      list(, $title, $pid) = $matches;
      if (!empty($title) && ($p = pet_load($pid)) && trim($title) != trim($p->title)) {
        form_error($element[$field_key], t('%name: title mismatch. Please check your selection.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
    }
    else {

      // No explicit nid.
      $reference = _petreference_potential_references($field, $value, 'equals', NULL, 1);
      if (empty($reference)) {
        form_error($element[$field_key], t('%name: found no valid post with that title.', 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...
        $pid = key($reference);
      }
    }
  }
  form_set_value($element, $pid, $form_state);
}