You are here

public function SensorThresholds::settingsFormValidate in Monitoring 7

Form validator for a sensor settings form.

Parameters

$form: Drupal $form structure.

array $form_state: Drupal $form_state object. Carrying the string sensor_name.

Overrides SensorConfigurable::settingsFormValidate

File

lib/Drupal/monitoring/Sensor/SensorThresholds.php, line 161
Contains \Drupal\monitoring\Sensor\SensorThresholds.

Class

SensorThresholds
Abstract class providing configuration form for Sensor with thresholds.

Namespace

Drupal\monitoring\Sensor

Code

public function settingsFormValidate($form, &$form_state) {
  $form_key = monitoring_sensor_settings_key($this->info
    ->getName());
  $values = $form_state['values'][$form_key]['thresholds'];
  $type = $values['type'];
  switch ($type) {
    case 'exceeds':
      if (!empty($values['warning']) && !empty($values['critical']) && $values['warning'] >= $values['critical']) {
        $this
          ->setFormError('warning', t('Warning must be lower than critical or empty.'));
      }
      break;
    case 'falls':
      if (!empty($values['warning']) && !empty($values['critical']) && $values['warning'] <= $values['critical']) {
        $this
          ->setFormError('warning', 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
          ->setFormError('warning_low', 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
          ->setFormError('critical_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_high', 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
          ->setFormError('warning_low', 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
          ->setFormError('critical_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_low', 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
          ->setFormError('warning_high', t('Warning high must be lower than critical high or empty.'));
      }
      break;
  }
}