You are here

function uc_webform_autocomplete_validate in Ubercart Webform Integration 7

Validation callback for a node_reference autocomplete element.

1 string reference to 'uc_webform_autocomplete_validate'
uc_webform_form_node_form_alter in ./uc_webform.module
Implements hook_form_FORM_ID_alter

File

./uc_webform.module, line 189

Code

function uc_webform_autocomplete_validate($element, &$form_state, $form) {
  $value = $element['#value'];
  $nid = NULL;
  if (!empty($value)) {

    // Check whether we have an explicit "[nid:n]" input.
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // Explicit nid. Check that the 'title' part matches the actual title for
      // the nid.
      list(, $title, $nid) = $matches;
      if (!empty($title)) {
        $real_title = db_select('node', 'n')
          ->fields('n', array(
          'title',
        ))
          ->condition('n.nid', $nid)
          ->execute()
          ->fetchField();
        if (trim($title) != trim($real_title)) {
          form_error($element, t('%name: title mismatch. Please check your selection.', array(
            '%name' => $instance['label'],
          )));
        }
      }
    }
  }
  form_set_value($element, $nid, $form_state);
}