You are here

public function SettingsForm::validateForm in Auto Purge Users 8

Same name and namespace in other branches
  1. 8.3 src/Form/SettingsForm.php \Drupal\purge_users\Form\SettingsForm::validateForm()
  2. 8.2 src/Form/SettingsForm.php \Drupal\purge_users\Form\SettingsForm::validateForm()

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/SettingsForm.php, line 270

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\purge_users\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $login_never_value = $form_state
    ->getValue([
    'user_never_lastlogin_value',
  ]);
  $login_never_period = $form_state
    ->getValue([
    'user_never_lastlogin_period',
  ]);
  $login_value = $form_state
    ->getValue([
    'user_lastlogin_value',
  ]);
  $login_period = $form_state
    ->getValue([
    'user_lastlogin_period',
  ]);
  $inactive_value = $form_state
    ->getValue([
    'user_inactive_value',
  ]);
  $inactive_period = $form_state
    ->getValue([
    'user_inactive_period',
  ]);
  $block_value = $form_state
    ->getValue([
    'user_blocked_value',
  ]);
  $block_period = $form_state
    ->getValue([
    'user_blocked_period',
  ]);
  $enable_blocked = $form_state
    ->getValue([
    'enabled_blocked_users',
  ]);
  $enable_loggedin = $form_state
    ->getValue([
    'enabled_loggedin_users',
  ]);
  $enable_never_loggedin = $form_state
    ->getValue([
    'enabled_never_loggedin_users',
  ]);
  $enable_inactive = $form_state
    ->getValue([
    'enabled_inactive_users',
  ]);

  // Validate text field to only contain numeric values.
  if ($login_never_value != '' && !is_numeric($login_never_value) || $login_value != '' && !is_numeric($login_value) || $inactive_value != '' && !is_numeric($inactive_value) || $block_value != '' && !is_numeric($block_value)) {
    $form_state
      ->setErrorByName('Value validator', $this
      ->t('Value must be a number.'));
  }

  // Validate to set purge period more than 10 days.
  if ($login_never_period == 'days' && !empty($login_never_value) && $login_never_value <= 10 || $login_period == 'days' && !empty($login_value) && $login_value <= 10 || $inactive_period == 'days' && !empty($inactive_value) && $inactive_value <= 10 || $block_period == 'days' && !empty($block_value) && $block_value <= 10) {
    $form_state
      ->setErrorByName('Period limit', $this
      ->t('Purge period should be more than 10 days.'));
  }

  // Make sure one of the fieldset is checked.
  if ($enable_loggedin == 0 && $enable_inactive == 0 && $enable_blocked == 0 && $enable_never_loggedin == 0) {
    $form_state
      ->setErrorByName('Enable fieldset', $this
      ->t('Please enable one of the Conditions:  Never logged in users, Not logged in users, Inactive users or Blocked users.'));
  }

  // Check if value field is not empty.
  if ($enable_blocked == 1 && empty($block_value) || $enable_loggedin == 1 && empty($login_value) || $enable_never_loggedin == 1 && empty($login_never_value) || $enable_inactive == 1 && empty($inactive_value)) {
    $form_state
      ->setErrorByName('Empty value field', $this
      ->t('Value should not be empty.'));
  }
}