You are here

function ip_geoloc_range_widget_validate in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc.module \ip_geoloc_range_widget_validate()

FAPI validation of a range element.

We want to cover numeric and alphabetic ranges, as well as the special replacement strings !1, !2 ... So we can't be very strict.

3 string references to 'ip_geoloc_range_widget_validate'
IpGeoLocPluginStyleLeaflet::addClusterDifferentiator in src/Plugin/views/style/IpGeoLocPluginStyleLeaflet.php
Form part definition.
IpGeoLocViewsPluginStyle::pluginStyleDiffColorTableRowForm in src/Services/IpGeoLocViewsPluginStyle.php
Diffs the color table plugin style row.
_ip_geoloc_plugin_style_diff_color_table_row_form in src/Plugin/views/style/ip_geoloc_plugin_style.inc
Diffs the color table plugin style row.

File

./ip_geoloc.module, line 723
IPGV&M is a mapping engine for Views that contain locations of entities and/or visitors. Google Maps, Leaflet and OpenLayers2 maps are all supported and available through this module. Using a number of optional sources IPGV&M also retrieves…

Code

function ip_geoloc_range_widget_validate($element, &$form_state) {
  $range = $element['#value'];
  $from_to = explode(IP_GEOLOC_RANGE_SEPARATOR1, $range);
  if (count($from_to) < 2) {
    $from_to = explode(IP_GEOLOC_RANGE_SEPARATOR2, $range);
  }
  if (count($from_to) < 2) {

    // Not a range but a single value. This is ok. If we knew we were checking
    // for a number we would pass the input through is_numeric(), but we don't.
  }
  else {
    $from = trim($from_to[0]);
    $to = trim($from_to[1]);
    if (preg_match('/^![0-9]/', $from) || preg_match('/^![0-9]/', $to)) {
      return;
    }
    $ok = TRUE;

    // If either $from or $to is numeric then assume numeric range and apply
    // validation accordingly.
    if (is_numeric($from) || is_numeric($to)) {

      // If one end is numeric, then the other must also be, or be empty.
      $ok = empty($from) && empty($to) || empty($from) && is_numeric($to) || empty($to) && is_numeric($from) || is_numeric($from) && is_numeric($to) && $from <= $to;
    }
    elseif (!empty($from) && !empty($to)) {

      // Alphabetic range validation.
      $ok = $from <= $to;
    }
    if (!$ok) {
      form_error($element, t('Invalid range.'));
    }
  }
}