You are here

function addressfield_staticmap_admin_form_validate in Address Field Static Map 7

Admin form validation callback.

Parameters

array $form: The built form.

array &$form_state: The form state.

File

./addressfield_staticmap.admin.inc, line 311
Contains admin page code for the addressfield static map module.

Code

function addressfield_staticmap_admin_form_validate(array $form, array &$form_state) {
  for ($index = 0; $index <= 1; $index++) {
    $zoom = $form_state['values']['addressfield_staticmap_gmap_zoom_' . $index];
    $api = $form_state['values']['addressfield_staticmap_api_' . $index];
    if ($api == 'google_maps') {
      if (!in_array($zoom, array(
        'auto' => t('Auto'),
      ) + drupal_map_assoc(range(0, 21)))) {
        form_set_error('addressfield_staticmap_gmap_zoom_' . $index, t('Invalid zoom level. Please select auto or a value between 0 and 21 inclusive'));
        return;
      }
    }
    elseif ($api == 'mapquest') {
      if (!in_array($zoom, drupal_map_assoc(range(1, 16)))) {
        form_set_error('addressfield_staticmap_gmap_zoom_' . $index, t('Invalid zoom level. Please select a value between 1 and 16 inclusive.'));
        return;
      }
    }
    $size = $form_state['values']['addressfield_staticmap_gmap_size_' . $index];
    if (!empty($size) && !preg_match('/^([0-9]+)x([0-9]+)$/', $size)) {
      form_set_error('addressfield_staticmap_gmap_size_' . $index, t('Invalid image size. Enter a size for the map in pixels (eg \'400x400\') or leave blank for the default.<br />When using Google\'s static maps, the size will default to 400x400 if left blank.<br />When using non-static maps, a blank value can be used to apply CSS styling.'));
      return;
    }
    $icon_url = $form_state['values']['addressfield_staticmap_gmap_icon_url_' . $index];
    if (!empty($icon_url) && !valid_url($icon_url)) {
      form_set_error('addressfield_staticmap_gmap_icon_url_' . $index, t('Invalid marker icon URL.'));
      return;
    }

    // Mapquest requires an API key.
    $api_key = $form_state['values']['addressfield_staticmap_api_key_' . $index];
    if ($api == 'mapquest' && empty($api_key)) {
      form_set_error('addressfield_staticmap_api_key_' . $index, t('Mapquest requires an API key.'));
      return;
    }
  }
}