You are here

public function SensorForm::validateThresholdsForm in Monitoring 8

1 call to SensorForm::validateThresholdsForm()
SensorForm::validateForm in src/Form/SensorForm.php
Form validation handler.

File

src/Form/SensorForm.php, line 430
Contains \Drupal\monitoring\Form\SensorForm.

Class

SensorForm
Sensor settings form controller.

Namespace

Drupal\monitoring\Form

Code

public function validateThresholdsForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValue(array(
    'thresholds',
  ));
  $type = is_array($values) ? $values['type'] : NULL;
  switch ($type) {
    case 'exceeds':
      if (!empty($values['warning']) && !empty($values['critical']) && $values['warning'] >= $values['critical']) {
        $this
          ->setThresholdFormError('warning', $form_state, $this
          ->t('Warning must be lower than critical or empty.'));
      }
      break;
    case 'falls':
      if (!empty($values['warning']) && !empty($values['critical']) && $values['warning'] <= $values['critical']) {
        $this
          ->setThresholdFormError('warning', $form_state, $this
          ->t('Warning must be higher than critical or empty.'));
      }
      break;
    case 'inner_interval':
      if (empty($values['warning_low']) && !empty($values['warning_high']) || !empty($values['warning_low']) && empty($values['warning_high'])) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Either both warning values must be provided or none.'));
      }
      elseif (empty($values['critical_low']) && !empty($values['critical_high']) || !empty($values['critical_low']) && empty($values['critical_high'])) {
        $this
          ->setThresholdFormError('critical_low', $form_state, $this
          ->t('Either both critical values must be provided or none.'));
      }
      elseif (!empty($values['warning_low']) && !empty($values['warning_high']) && $values['warning_low'] >= $values['warning_high']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Warning low must be lower than warning high or empty.'));
      }
      elseif (!empty($values['critical_low']) && !empty($values['critical_high']) && $values['critical_low'] >= $values['critical_high']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Critical low must be lower than critical high or empty.'));
      }
      elseif (!empty($values['warning_low']) && !empty($values['critical_low']) && $values['warning_low'] >= $values['critical_low']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Warning low must be lower than critical low or empty.'));
      }
      elseif (!empty($values['warning_high']) && !empty($values['critical_high']) && $values['warning_high'] <= $values['critical_high']) {
        $this
          ->setThresholdFormError('warning_high', $form_state, $this
          ->t('Warning high must be higher than critical high or empty.'));
      }
      break;
    case 'outer_interval':
      if (empty($values['warning_low']) && !empty($values['warning_high']) || !empty($values['warning_low']) && empty($values['warning_high'])) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Either both warning values must be provided or none.'));
      }
      elseif (empty($values['critical_low']) && !empty($values['critical_high']) || !empty($values['critical_low']) && empty($values['critical_high'])) {
        $this
          ->setThresholdFormError('critical_low', $form_state, $this
          ->t('Either both critical values must be provided or none.'));
      }
      elseif (!empty($values['warning_low']) && !empty($values['warning_high']) && $values['warning_low'] >= $values['warning_high']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Warning low must be lower than warning high or empty.'));
      }
      elseif (!empty($values['critical_low']) && !empty($values['critical_high']) && $values['critical_low'] >= $values['critical_high']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Critical low must be lower than critical high or empty.'));
      }
      elseif (!empty($values['warning_low']) && !empty($values['critical_low']) && $values['warning_low'] <= $values['critical_low']) {
        $this
          ->setThresholdFormError('warning_low', $form_state, $this
          ->t('Warning low must be higher than critical low or empty.'));
      }
      elseif (!empty($values['warning_high']) && !empty($values['critical_high']) && $values['warning_high'] >= $values['critical_high']) {
        $this
          ->setThresholdFormError('warning_high', $form_state, $this
          ->t('Warning high must be lower than critical high or empty.'));
      }
      break;
  }
}