You are here

public function ShrinkTheWebSettingsForm::validateForm in ShrinkTheWeb 8

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/ShrinkTheWebSettingsForm.php, line 283

Class

ShrinkTheWebSettingsForm

Namespace

Drupal\shrinktheweb\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setValue('shrinktheweb_access_key', trim($form_state
    ->getValue([
    'shrinktheweb_access_key',
  ])));
  $form_state
    ->setValue('shrinktheweb_secret_key', trim($form_state
    ->getValue([
    'shrinktheweb_secret_key',
  ])));
  $form_state
    ->setValue('shrinktheweb_cache_days', trim($form_state
    ->getValue([
    'shrinktheweb_cache_days',
  ])));
  $form_state
    ->setValue('shrinktheweb_thumb_size_custom', trim($form_state
    ->getValue([
    'shrinktheweb_thumb_size_custom',
  ])));
  $form_state
    ->setValue('shrinktheweb_max_height', trim($form_state
    ->getValue([
    'shrinktheweb_max_height',
  ])));
  $form_state
    ->setValue('shrinktheweb_native_res', trim($form_state
    ->getValue([
    'shrinktheweb_native_res',
  ])));
  $form_state
    ->setValue('shrinktheweb_widescreen_y', trim($form_state
    ->getValue([
    'shrinktheweb_widescreen_y',
  ])));
  $form_state
    ->setValue('shrinktheweb_delay', trim($form_state
    ->getValue([
    'shrinktheweb_delay',
  ])));
  $form_state
    ->setValue('shrinktheweb_quality', trim($form_state
    ->getValue([
    'shrinktheweb_quality',
  ])));
  $form_state
    ->setValue('shrinktheweb_custom_msg_url', trim($form_state
    ->getValue([
    'shrinktheweb_custom_msg_url',
  ])));
  $form_state
    ->setValue('shrinktheweb_token', trim($form_state
    ->getValue([
    'shrinktheweb_token',
  ])));
  module_load_include('inc', 'shrinktheweb', 'shrinktheweb.api');
  $aAccountInfo = shrinktheweb_getAccountInfo($form_state
    ->getValue([
    'shrinktheweb_access_key',
  ]), $form_state
    ->getValue([
    'shrinktheweb_secret_key',
  ]));
  $response_status = $aAccountInfo['stw_response_status'];
  if ($form_state
    ->getValue([
    'shrinktheweb_access_key',
  ]) == '' || $form_state
    ->getValue([
    'shrinktheweb_secret_key',
  ]) == '' || $response_status != 'Success') {
    $form_state
      ->setErrorByName('invalid_account_credentials', t('Invalid account credentials detected.'));
  }
  $cache_days = $form_state
    ->getValue([
    'shrinktheweb_cache_days',
  ]);
  if (!empty($cache_days)) {
    if (!is_numeric($cache_days)) {
      $form_state
        ->setErrorByName('shrinktheweb_cache_days', t('You must enter an integer for days in cache.'));
    }
    else {
      if ($cache_days < -1) {
        $form_state
          ->setErrorByName('shrinktheweb_cache_days', t('Number of days in cache must be greater then -1.'));
      }
    }
  }
  $thumb_size_custom = $form_state
    ->getValue([
    'shrinktheweb_thumb_size_custom',
  ]);
  if (!empty($thumb_size_custom)) {
    if (!is_numeric($thumb_size_custom)) {
      $form_state
        ->setErrorByName('shrinktheweb_thumb_size_custom', t('You must enter an integer for custom thumb size.'));
    }
    else {
      if ($thumb_size_custom < 0) {
        $form_state
          ->setErrorByName('shrinktheweb_thumb_size_custom', t('Custom thumb size must be positive.'));
      }
    }
  }
  $max_height = $form_state
    ->getValue([
    'shrinktheweb_max_height',
  ]);
  if (!empty($max_height)) {
    if (!is_numeric($max_height)) {
      $form_state
        ->setErrorByName('shrinktheweb_max_height', t('You must enter an integer for maximum height.'));
    }
    else {
      if ($max_height < 0) {
        $form_state
          ->setErrorByName('shrinktheweb_max_height', t('Maximum height must be positive.'));
      }
    }
  }
  $native_res = $form_state
    ->getValue([
    'shrinktheweb_native_res',
  ]);
  if (!empty($native_res)) {
    if (!is_numeric($native_res)) {
      $form_state
        ->setErrorByName('shrinktheweb_native_res', t('You must enter an integer for native resolution.'));
    }
    else {
      if ($native_res < 0) {
        $form_state
          ->setErrorByName('shrinktheweb_native_res', t('Native resolution must be positive.'));
      }
    }
  }
  $widescreen_y = $form_state
    ->getValue([
    'shrinktheweb_widescreen_y',
  ]);
  if (!empty($widescreen_y)) {
    if (!is_numeric($widescreen_y)) {
      $form_state
        ->setErrorByName('shrinktheweb_widescreen_y', t('You must enter an integer for widescreen Y.'));
    }
    else {
      if ($widescreen_y < 0) {
        $form_state
          ->setErrorByName('shrinktheweb_widescreen_y', t('Widescreen Y must be positive.'));
      }
    }
  }
  $delay = $form_state
    ->getValue([
    'shrinktheweb_delay',
  ]);
  if (!empty($delay)) {
    if (!is_numeric($delay)) {
      $form_state
        ->setErrorByName('shrinktheweb_delay', t('You must enter an integer for delay.'));
    }
    else {
      if ($delay < 0 || $delay > 45) {
        $form_state
          ->setErrorByName('shrinktheweb_delay', t('Delay must be between 0 and 45.'));
      }
    }
  }
  $quality = $form_state
    ->getValue([
    'shrinktheweb_quality',
  ]);
  if (!empty($quality)) {
    if (!is_numeric($quality)) {
      $form_state
        ->setErrorByName('shrinktheweb_quality', t('You must enter an integer for quality.'));
    }
    else {
      if ($quality < 0 || $quality > 100) {
        $form_state
          ->setErrorByName('shrinktheweb_quality', t('Quality must be between 0 and 100.'));
      }
    }
  }
  $token = $form_state
    ->getValue([
    'shrinktheweb_token',
  ]);
  if (!empty($token)) {
    if (mb_strlen($token) < 32) {
      $form_state
        ->setErrorByName('shrinktheweb_token', t('Token should contain 32 symbols'));
    }
  }
}