You are here

function yr_verdata_add_form_validate in Yr Weatherdata 7

Same name and namespace in other branches
  1. 6.2 yr_verdata.admin.inc \yr_verdata_add_form_validate()
  2. 7.3 yr_verdata.admin.inc \yr_verdata_add_form_validate()

Validation handler for yr_verdata_add_form().

File

./yr_verdata.admin.inc, line 219
This file contains the functions for the admin interface for yr_verdata.

Code

function yr_verdata_add_form_validate($form, &$form_state) {

  // Verify that the url entered is actually for yr.no.
  $start = drupal_substr($form_state['values']['url'], 0, 17);
  if ($start !== 'http://www.yr.no/') {
    form_set_error('url', t('The url must be a valid yr.no address.'));
  }

  // Check if the location already exists.
  $url = trim($form_state['values']['url']);

  // Make sure we are not using yr.no's cache, which sometimes adds numbers at the end of a url.
  $tilde = mb_strpos($url, '~');
  if ($tilde == TRUE) {
    $url = drupal_substr($url, 0, $tilde);
  }

  // Make sure we always have a trailing slash.
  if (drupal_substr($url, -1) != '/') {
    $url .= '/';
  }

  // Make sure we don't add a location twice if it's added from different browsers.
  // Some browsers automatically urlencode urls if you copy them from the address field.
  $url = rawurldecode($url);
  $result = db_query("SELECT yid FROM {yr_verdata} WHERE url = :url", array(
    ':url' => $url,
  ));
  if ($result
    ->fetch() == TRUE) {
    form_set_error('url', t('The location already exists in the database.'));
  }
}