function chado_featuremap_validate

2.x tripal_featuremap.chado_node.inc chado_featuremap_validate($node, $form, &$form_state)
3.x tripal_featuremap.chado_node.inc chado_featuremap_validate($node, $form, &$form_state)
1.x tripal_featuremap.form.inc chado_featuremap_validate($node, &$form)

Validates submission of form when adding or updating a map node

Related topics

File

tripal_featuremap/includes/tripal_featuremap.chado_node.inc, line 156
Hooks implementing the feature map node content type

Code

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

  if ($node->unittype_id == 0) {
    form_set_error('unittype_id', 'Please provide a unit type for this map.');
  }

  // trim white space from text fields
  $node->fmapname = property_exists($node, 'fmapname') ? trim($node->fmapname) : '';

  $featuremap = 0;
  // check to make sure the unique name on the map is unique
  // before we try to insert into chado. If this is an update then we will
  // have a featuremap_id, therefore we want to look for another map with this
  // name but with a different featuremap_id. If this is an insert, just look
  // for a case where the name already exists.
  if (property_exists($node, 'featuremap_id')) {
    $sql = "
      SELECT * FROM {featuremap}
      WHERE name = :name AND NOT featuremap_id = :featuremap_id
    ";
    $featuremap = chado_query($sql, array(':name' => $node->fmapname, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  }
  else {
    $sql = "SELECT * FROM {featuremap} WHERE name = :name";
    $featuremap = chado_query($sql, array(':name' => $node->fmapname))->fetchObject();
  }
  if ($featuremap) {
    form_set_error('fmapname', t('The unique map name already exists. Please choose another'));
  }
}