function tripal_custom_tables_form_validate

2.x tripal_core.custom_tables.inc tripal_custom_tables_form_validate($form, &$form_state)
3.x tripal_chado.custom_tables.inc tripal_custom_tables_form_validate($form, &$form_state)
1.x custom_tables.inc tripal_custom_tables_form_validate($form, &$form_state)

Implements hook_validate(). Validate the Create/Edit custom table form.

Related topics

File

tripal_core/includes/tripal_core.custom_tables.inc, line 290
Contains functions for creating, editing and deleting custom tables on the Tripal website.

Code

function tripal_custom_tables_form_validate($form, &$form_state) {
  $action = $form_state['values']['action'];
  $table_id = $form_state['values']['table_id'];
  $schema = $form_state['values']['schema'];
  $force_drop = $form_state['values']['force_drop'];

  if (!$schema) {
    form_set_error($form_state['values']['schema'], t('Schema array field is required.'));
  }

  // make sure the array is valid
  $schema_array = array();
  if ($schema) {
    $success = preg_match('/^\s*array/', $schema);
    if (!$success) {
      form_set_error($form_state['values']['schema'], 
      t("The schema array should begin with the word 'array'."));
    }
    else {
      $success = eval("\$schema_array = $schema;");
      if ($success === FALSE) {
        $error = error_get_last();
        form_set_error('schema', t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
      }
      if (is_array($schema_array) and !array_key_exists('table', $schema_array)) {
        form_set_error('schema', t("The schema array must have key named 'table'"));
      }

      // validate the contents of the array
      $error = chado_validate_custom_table_schema($schema_array);
      if ($error) {
        form_set_error('schema', $error);
      }

      if ($action == 'Edit') {
        // see if the table name has changed. If so, then check to make sure
        // it doesn't already exists. We don't want to drop a table we didn't mean to
        $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
        $results = db_query($sql, array(':table_id' => $table_id));
        $ct = $results->fetchObject();
        if ($ct->table_name != $schema_array['table']) {
          $exists = chado_table_exists($schema_array['table']);
          if ($exists) {
            form_set_error($form_state['values']['schema'], 
            t("The table name already exists, please choose a different name."));
          }
        }
      }
    }
  }
}