function chado_contact_validate

2.x tripal_contact.chado_node.inc chado_contact_validate($node, $form, &$form_state)
3.x tripal_contact.chado_node.inc chado_contact_validate($node, $form, &$form_state)
1.x tripal_contact.form.inc chado_contact_validate($node, &$form)

Implements hook_validate(). Validates submission of form when adding or updating a contact node.

Related topics

File

tripal_contact/includes/tripal_contact.chado_node.inc, line 247
Implements drupal node hooks.

Code

function chado_contact_validate($node, $form, &$form_state) {

  // We only want to validate when the node is saved.
  // Since this validate can be called on AJAX and Deletion of the node
  // we need to make this check to ensure queries are not executed
  // without the proper values.
  if (property_exists($node, "op") and $node->op != 'Save') {
    return;
  }

  // we are syncing if we do not have a node ID but we do have a contact_id. We don't
  // need to validate during syncing so just skip it.
  if (!property_exists($node, 'nid') and property_exists($node, 'contact_id') and $node->contact_id != 0) {
    return;
  }

  // remove surrounding white-space on submitted values
  $node->contactname = property_exists($node, 'contactname') ? trim($node->contactname) : '';

  // Validating for an update
  if (!is_null($node->nid)) {
    // get the existing node
    $values = array('contact_id' => $node->contact_id);
    $result = chado_select_record('contact', array('*'), $values);
    $contact = $result[0];

    // if the name has changed make sure it doesn't conflict with an existing name
    if ($contact->name != $node->contactname) {
      $values = array('name' => $node->contactname);
      $result = chado_select_record('contact', array('contact_id'), $values);
      if ($result and count($result) > 0) {
        form_set_error('contactname', 'Cannot update the contact with this contact name. A contact with this name already exists.');
        return;
      }
    }
  }
  // Validating for an insert
  else {
    // The unique constraint for the chado contact table is: name
    $values = array(
      'name' => $node->contactname,
    );
    $contact = chado_select_record('contact', array('contact_id'), $values);
    if ($contact and count($contact) > 0) {
      form_set_error('contactname', 'Cannot add the contact with this name. A contact with these values already exists.');
      return;
    }
  }
}