function chado_example_validate

2.x tripal_example.chado_node.inc chado_example_validate($node, $form, &$form_state)

Implementation of hook_validate

This function validates a form prior to insert or update. If an error is detected, it sets the error using form_set_error() which takes the user back to the form to make corrections.

This validation is being used for three activities: CASE A: Update a node that exists in both Drupal and Chado CASE B: Synchronizing a node from Chado to Drupal CASE C: Inserting a new node that exists in neither Drupal nor Chado

Parameters

$node:

File

tripal_example/includes/tripal_example.chado_node.inc, line 309
This file should contain all Drupal hooks for interacting with nodes.

Code

function chado_example_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 example_id. We
  // don't need to validate during syncing so just skip it.
  if (!property_exists($node, 'nid') and property_exists($node, 'example_id') and $node->example_id != 0) {
    return;
  }

  // be sure to always trim text fields
  $node->uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';

  // Validating for an update. If the 'nid' property is present in the node then
  // this is an update and validation can be different for updates
  if (property_exists($node, 'nid')) {

    // make sure the example type is an allowed term
    $type_cv = tripal_get_default_cv('example', 'type_id');
    $type = tripal_get_cvterm(array(
      'name' => $node->example_type,
      'cv_id' => $type_cv->cv_id,
    ));
    if (!$type) {
      form_set_error('example_type', t("The example type is not a valid name from the Sequence Ontology."));
    }

    // TODO: also we should check that the unique constraint is not invalidated
    // by changing either the type_id, organism_id or uniquename.
  }
  // Validating for an insert
  else {
    // make sure the example type is an allowed term
    $type_cv = tripal_get_default_cv('example', 'type_id');
    $type = tripal_get_cvterm(array(
      'name' => $node->example_type,
      'cv_id' => $type_cv->cv_id,
    ));
    if (!$type) {
      form_set_error('example_type', t("The example type is not a valid name from the Sequence Ontology."));
    }

    // TODO: also we should check that the unique constraint doesn't already exist
  }
}