You are here

function openlayers_ui_presets_form_validate in Openlayers 6.2

OpenLayers Preset Form Validate

Validates a preset form submission.

Parameters

$map_form: Array of values to validate

Return value

Does not return anything. Uses form_set_error() to communicate errors.

File

modules/openlayers_ui/includes/openlayers_ui.presets.inc, line 416
This file holds the functions handling presets in the Openlayers UI.

Code

function openlayers_ui_presets_form_validate($form, &$form_state) {
  $values = $form_state['values'];
  $found_error = FALSE;

  // Check if ahah submitting
  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-projection-ahah') {
    return TRUE;
  }

  // Check for cancel
  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-cancel') {
    return TRUE;
  }

  // Check for values.  We manually do required fields because, it would otherwise
  // mess with the AHAH stuff.  Maybe a way around it.
  if (empty($values['name'])) {
    form_set_error('name', t('Preset name is required.'));
    $found_error = TRUE;
  }
  if (empty($values['title'])) {
    form_set_error('title', t('Preset title is required.'));
    $found_error = TRUE;
  }
  if (empty($values['description'])) {
    form_set_error('description', t('Preset description is required.'));
    $found_error = TRUE;
  }
  if (empty($values['width'])) {
    form_set_error('width', t('Width is required.'));
    $found_error = TRUE;
  }
  if (empty($values['height'])) {
    form_set_error('height', t('Height is required.'));
    $found_error = TRUE;
  }

  // Check preset name first
  if (!preg_match('!^[a-z0-9_]+$!', $values['name'])) {
    form_set_error('name', t('Preset Name must contain only lowercase letters, numbers, and underscores.'));
    $found_error = TRUE;
  }

  // Check if adding and name already exists
  $existing = openlayers_preset_load($values['name'], TRUE);
  if (!empty($existing) && !$form_state['values']['preset_edit'] && $existing->export_type & EXPORT_IN_DATABASE) {
    form_set_error('name', t('The Preset Name already exists.'));
    $found_error = TRUE;
  }

  // Attempt to render map to find any errors
  $map = openlayers_ui_presets_form_process($form_state['values']);
  $map = openlayers_build_map($map);

  // Check if any errors found
  if (is_array($map['errors']) && count($map['errors']) > 0) {
    foreach ($map['errors'] as $error) {
      form_set_error('openlayers', t('OpenLayers Map Rendering Error: !error', array(
        '!error' => $error,
      )));
      $found_error = TRUE;
    }
  }

  // If found error, rebuild form
  if ($found_error) {

    // Add OpenLayers CSS & Javascript, because the form function will not be run on rebuild
    openlayers_include();
    drupal_add_js(drupal_get_path('module', 'openlayers_ui') . '/js/openlayers_ui.presets.js', 'module');
    drupal_add_css(drupal_get_path('module', 'openlayers_ui') . '/openlayers_ui.css');
    $form_state['rebuild'] = TRUE;
  }
}