You are here

function _field_ipaddress_validate_long in IP address fields 7

Validate a long input.

1 string reference to '_field_ipaddress_validate_long'
field_ipaddress_field_widget_form in ./field_ipaddress.module
Implements hook_field_widget_form().

File

./field_ipaddress.module, line 195

Code

function _field_ipaddress_validate_long($element, &$form_state) {
  $delta = $element['#delta'];
  $lang_code = $element['#language'];
  $field = $form_state['field'][$element['#field_name']][$lang_code]['field'];
  $field_name = $field['field_name'];
  $startip = trim($form_state['values'][$field_name][$lang_code][$delta]['start']);
  $endip = trim($form_state['values'][$field_name][$lang_code][$delta]['end']);
  if (!empty($startip)) {
    $start = ip2long($startip);
    if (empty($endip)) {
      $end = $start;
    }
    else {
      $end = ip2long($endip);
    }
    $startip = long2ip($start);
    $endip = long2ip($end);
    if ($startip === '0.0.0.0' || $endip === '0.0.0.0') {
      form_error($element, t('Invalid start or end IP.'));
    }
    else {
      if ($start > $end) {
        form_error($element, t('Wrong order of start and end IP.'));
      }
      else {
        form_set_value($element['start'], $start, $form_state);
        form_set_value($element['end'], $end, $form_state);
      }
    }
  }
}