function chado_organism_validate

2.x tripal_organism.chado_node.inc chado_organism_validate($node, $form, &$form_state)
3.x tripal_organism.chado_node.inc chado_organism_validate($node, $form, &$form_state)
1.x tripal_organism.module chado_organism_validate($node, &$form)

Implementation of hook_validate().

_state

Parameters

$node:

$form:

Related topics

File

tripal_organism/includes/tripal_organism.chado_node.inc, line 301
Implements the organims node content type

Code

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

  // remove any white space around values
  $node->genus = property_exists($node, 'genus') ? trim($node->genus) : '';
  $node->species = property_exists($node, 'species') ? trim($node->species) : '';
  $node->abbreviation = property_exists($node, 'abbreviation') ? trim($node->abbreviation) : '';
  $node->common_name = property_exists($node, 'common_name') ? trim($node->common_name) : '';
  $node->type_id = property_exists($node, 'type_id') ? trim($node->type_id) : '';
  $node->infraspecific_name = property_exists($node, 'infraspecific_name') ? trim($node->infraspecific_name) : '';

  if ($node->type_id and !$node->infraspecific_name) {
    form_set_error('infraspecific_name', "If a rank is provided an infraspecific name must also be provided.");
  }
  if (!$node->type_id and $node->infraspecific_name) {
    form_set_error('type_id', "Please provide a rank for the infraspecific name.");
  }

  // Validating for an update
  if (property_exists($node, 'organism_id')) {
    $sql = "
      SELECT *
      FROM {organism} O
      WHERE
        genus = :genus AND
        species = :species AND NOT
        organism_id = :organism_id
    ";
    $args = array(':genus' => $node->genus, ':species' => $node->species, ':organism_id' => $node->organism_id);
    $result = chado_query($sql, $args)->fetchObject();
    if ($result) {
      form_set_error('genus', t("Update cannot proceed. The organism genus
        '$node->genus' and species '$node->species' is already present in the database."));
      tripal_report_error('tripal_organism', TRIPAL_WARNING, 
      'Update organism: genus and species already exists: %values', 
      array('%values' => "genus = $node->genus, species = $node->species"));
    }
  }
  // Validating for an insert
  else {
    $values = array(
      'genus' => $node->genus,
      'species' => $node->species,
    );
    $organism = chado_select_record('organism', array('organism_id'), $values);
    if (sizeof($organism) > 0) {
      form_set_error('genus', 'Cannot add the organism with this genus and species.
        The organism already exists.');
      tripal_report_error('tripal_organism', TRIPAL_WARNING, 
      'Insert organism: genus and species already exists: %values', 
      array('%values' => "genus = $node->genus, species = $node->species"));
    }
  }
}