You are here

function location_extra_form_validate in Location 5

File

./location.module, line 1155

Code

function location_extra_form_validate($form_id, &$form_values) {
  foreach ($form_values['location'] as $key => $value) {
    $form_values['location'][$key] = trim($value);
  }

  // Check if the (province, country) pair is valid
  if (isset($form_values['location']['province']) && !empty($form_values['location']['province']) && $form_values['location']['province'] != 'xx' && isset($form_values['location']['country']) && !empty($form_values['location']['country']) && $form_values['location']['country'] != 'xx') {
    $province_list_function = 'location_province_list_' . $form_values['location']['country'];
    if (function_exists($province_list_function)) {
      $translated_location = location_form2api($form_values['location']);
      if (!in_array($translated_location['province'], array_keys($province_list_function()))) {
        form_set_error('location][province', t('Please make sure to select a state/province from the country you have selected.'));
      }
    }
  }

  // Check if submitted lat/lon are valid
  if (user_access('submit latitude/longitude')) {
    if (!strlen(trim($form_values['location']['latitude'])) && strlen(trim($form_values['location']['longitude'])) || strlen(trim($form_values['location']['latitude'])) && !strlen(trim($form_values['location']['longitude']))) {
      form_set_error('location][latitude', t('You must fill out both longitude and latitude or'));
      form_set_error('location][longitude', t('you must leave them both blank.'));
    }
    elseif (strlen(trim($form_values['location']['latitude'])) && strlen(trim($form_values['location']['longitude']))) {
      if (!is_numeric($form_values['location']['latitude']) || $form_values['location']['latitude'] > 90.0 || $form_values['location']['latitude'] < -90.0) {
        form_set_error('location][latitude', t('Your latitude must be a numeric value between -90.0 and 90.0.'));
      }
      if (!is_numeric($form_values['location']['longitude']) || $form_values['location']['longitude'] > 180.0 || $form_values['location']['longitude'] < -180.0) {
        form_set_error('location][longitude', t('Your longitude must be a numeric value between -180.0 and 180.0.'));
      }
    }
  }
}