public function UserSettingsForm::validateForm in Restrict Login or Role Access by IP Address 8.4
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormBase::validateForm
File
- src/
Form/ UserSettingsForm.php, line 85
Class
- UserSettingsForm
- Class UserSettingsForm.
Namespace
Drupal\restrict_by_ip\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$config = $this
->config('restrict_by_ip.settings');
// Validation for existing restrictions.
foreach ($form_state
->getValues() as $key => $value) {
if (strpos($key, 'restrict_by_ip_user_') !== FALSE && strlen($value) > 0) {
$ip_addresses = explode(";", $value);
foreach ($ip_addresses as $ip) {
try {
$this->ip_tools
->validateIP($ip);
} catch (InvalidIPException $e) {
$form_state
->setErrorByName($key, $this
->t($e
->getMessage() . ' @ip.', [
'@ip' => $ip,
]));
}
}
}
}
// Validation for new restriction.
if (strlen($form_state
->getValue('name')) > 0) {
// Validate no existing restriction.
if ($config
->get('user.' . $form_state
->getValue('name')) !== NULL) {
$form_state
->setErrorByName('name', $this
->t('Restriction for that user already exist.'));
}
// Validate restriction.
$ip_addresses = explode(";", $form_state
->getValue('restriction'));
foreach ($ip_addresses as $ip) {
try {
$this->ip_tools
->validateIP($ip);
} catch (InvalidIPException $e) {
$form_state
->setErrorByName('restriction', $this
->t($e
->getMessage() . ' @ip.', [
'@ip' => $ip,
]));
}
}
}
}