You are here

private function IpBanAdmin::iPBanValidateIPs in IP Ban 8

Custom validation function for valid IP addresses.

Custom validation function for the list of additional IP addresses to either ban or mark as read-only. We convert the textarea into an array of IP addresses, then check if each address is valid. If any one line is invalid, we set the entire form element to invalid.

1 call to IpBanAdmin::iPBanValidateIPs()
IpBanAdmin::validateForm in src/Form/IpBanAdmin.php
Form validation handler.

File

src/Form/IpBanAdmin.php, line 206

Class

IpBanAdmin

Namespace

Drupal\ip_ban\Form

Code

private function iPBanValidateIPs($form_element, $form_value, array &$form, FormStateInterface $form_state) {
  if (!empty($form_value)) {
    $ip_array = explode(PHP_EOL, $form_value);
    foreach ($ip_array as $ip) {
      if (filter_var(trim($ip), FILTER_VALIDATE_IP) == FALSE) {
        $form_state
          ->setErrorByName($form_element, t('You have entered one or more incorrect IPV4 addresses.'));
      }
    }
  }
}