public function schema__publication_widget::validate

3.x schema__publication_widget.inc public schema__publication_widget::validate($element, $form, &$form_state, $langcode, $delta)

Overrides TripalFieldWidget::validate

See also

TripalFieldWidget::submit()

File

tripal_chado/includes/TripalFields/schema__publication/schema__publication_widget.inc, line 91

Class

schema__publication_widget

Code

public function validate($element, $form, &$form_state, $langcode, $delta) {

  // Get the FK column that links to the base table.
  $table_name = $this->instance['settings']['chado_table'];
  $base_table = $this->instance['settings']['base_table'];
  $schema = chado_get_schema($table_name);
  $pkey = $schema['primary key'][0];
  $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  $fkey = $fkeys[0];
  $field_name = $this->field['field_name'];

  // Get the field values.
  $fkey_value = $form_state['values'][$field_name]['und'][$delta]['value'];
  $pub_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'];
  $title = $form_state['values'][$field_name]['und'][$delta]['pub_title'];

  // If the user provided a pub_title then we want to set the foreign key
  // value to be the chado_record_id
  if ($title) {
    $matches = array();
    if (preg_match('/^.*\[id:(\d+)]$/', $title, $matches)) {
      $pub_id = $matches[1];
      $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
      $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
      $form_state['values'][$field_name]['und'][$delta]['value'] = $pub->pub_id;
    }
  }

  // In the widgetFrom function we automatically add the foreign key
  // record.  But if the user did not provide a publication we want to take
  // it out so that the Chado field_storage infrastructure won't try to
  // write a record.
  if (!$title) {
    $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
    $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__' . $fkey] = '';
    $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'] = '';
  }
}