function tripal_mviews_form_validate

2.x tripal_core.mviews.inc tripal_mviews_form_validate($form, &$form_state)
3.x tripal_chado.mviews.inc tripal_mviews_form_validate($form, &$form_state)
1.x mviews.inc tripal_mviews_form_validate($form, &$form_state)

Validate the Create/Edit Materialized View Form Implements hook_form_validate().

Related topics

File

tripal_core/includes/tripal_core.mviews.inc, line 468
Contains functions for viewing and editing of Materialized Views on a Tripal website.

Code

function tripal_mviews_form_validate($form, &$form_state) {


  $action = $form_state['values']['action'];
  $mview_id = $form_state['values']['mview_id'];
  $name = trim($form_state['values']['name']);
  $is_legacy = $form_state['values']['is_legacy'];
  $query = $form_state['values']['mvquery'];

  // if this is a legacy materialized view (no longer supported in Tripal v2.0
  // but code left just in case)
  if ($is_legacy) {
    $mv_table = trim($form_state['values']['mv_table']);
    $mv_specs = $form_state['values']['mv_specs'];
    $indexed = $form_state['values']['indexed'];
    $special_index = ''; //$form_state['values']['special_index'];
  }
  else {
    $mv_table = '';
    $mv_specs = '';
    $indexed = '';
    $special_index = '';
  }
  $comment = trim($form_state['values']['comment']);
  $schema = $form_state['values']['schema'];

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

  // if both the schema and the older fields for the legacy view are populated then
  // this is an error and we need to let the user know.
  if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
    form_set_error($form_state['values']['schema'], 
    t('You can create an MView using the Drupal Schema API method or the ' .
      'traditional method but not both.'));
  }

  // if we don't have a schema and are missing fields for the legacy views then
  // inform the user.
  if (!$schema) {
    if (!$mv_specs) {
      form_set_error($form_state['values']['mv_specs'], 
      t('The Table Definition field is required.'));
    }
    if (!$mv_table) {
      form_set_error($form_state['values']['mv_table'], 
      t('The Table Name field is required.'));
    }
  }

  // make sure the array is valid
  if ($schema) {
    $success = eval("\$schema_array = $schema;");
    if ($success === FALSE) {
      $error = error_get_last();
      form_set_error($form_state['values']['schema'], 
      t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
    }
    if (!array_key_exists('table', $schema_array)) {
      form_set_error($form_state['values']['schema'], 
      t("The schema array must have key named 'table'"));
    }
    // TODO: add in more validation checks of the array to help the user
  }
}