You are here

public function Lightbox2GeneralSettingsForm::validateForm in Lightbox2 8

Same name in this branch
  1. 8 src/Lightbox2GeneralSettingsForm.php \Drupal\lightbox2\Lightbox2GeneralSettingsForm::validateForm()
  2. 8 src/Form/Lightbox2GeneralSettingsForm.php \Drupal\lightbox2\Form\Lightbox2GeneralSettingsForm::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/Lightbox2GeneralSettingsForm.php, line 539
Contains \Drupal\lightbox2\Form\Lightbox2GeneralSettingsForm.

Class

Lightbox2GeneralSettingsForm

Namespace

Drupal\lightbox2\Form

Code

public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  if ($form_state
    ->getValue([
    'op',
  ]) == t('Save configuration')) {
    $border_size = $form_state
      ->getValue([
      'lightbox2_border_size',
    ]);
    $box_hex_colour = $form_state
      ->getValue([
      'lightbox2_box_color',
    ]);
    $font_hex_colour = $form_state
      ->getValue([
      'lightbox2_font_color',
    ]);
    $top_position = $form_state
      ->getValue([
      'lightbox2_top_position',
    ]);
    $overlay_hex_colour = $form_state
      ->getValue([
      'lightbox2_overlay_color',
    ]);
    $resize_speed = $form_state
      ->getValue([
      'lightbox2_resize_speed',
    ]);
    $fadein_speed = $form_state
      ->getValue([
      'lightbox2_fadein_speed',
    ]);
    $slide_down_speed = $form_state
      ->getValue([
      'lightbox2_slidedown_speed',
    ]);
    $flv_player_path = $form_state
      ->getValue([
      'lightbox2_flv_player_path',
    ]);
    if (!empty($flv_player_path) && $form_state
      ->getValue([
      'lightbox2_enable_video',
    ])) {
      if (strpos($flv_player_path, base_path()) === 0) {
        $flv_player_path = drupal_substr($flv_player_path, drupal_strlen(base_path()));
      }
      if (!file_exists($flv_player_path)) {
        $form_state
          ->setErrorByName('lightbox2_flv_player_path', t("FLV player path doesn't exist."));
      }
    }
    if (!_lightbox2_validate_hex_color($overlay_hex_colour)) {
      $form_state
        ->setErrorByName('lightbox2_overlay_color', t('You must enter a properly formed hex value.'));
    }
    if (!$form_state
      ->getValue([
      'lightbox2_lite',
    ])) {
      if (!is_numeric($border_size) || $border_size < 0) {
        $form_state
          ->setErrorByName('lightbox2_border_size', t('You must enter a size greater than 0 pixels.'));
      }
      if (!_lightbox2_validate_hex_color($box_hex_colour)) {
        $form_state
          ->setErrorByName('lightbox2_box_color', t('You must enter a properly formed hex value.'));
      }
      if (!_lightbox2_validate_hex_color($font_hex_colour)) {
        $form_state
          ->setErrorByName('lightbox2_font_color', t('You must enter a properly formed hex value.'));
      }
      if (!empty($top_position) && (!is_numeric($top_position) || $top_position < 0)) {
        $form_state
          ->setErrorByName('lightbox2_top_position', t('You must enter a size greater than 0 pixels.  Leave blank for default positioning.'));
      }
      if (!is_numeric($resize_speed) || $resize_speed <= 0 || $resize_speed >= 10) {
        $form_state
          ->setErrorByName('lightbox2_resize_speed', t('You must enter a duration between 0 and 10 seconds.'));
      }
      if (!is_numeric($fadein_speed) || $fadein_speed < 0 || $fadein_speed >= 10) {
        $form_state
          ->setErrorByName('lightbox2_fadein_speed', t('You must enter a duration between 0 and 10 seconds.'));
      }
      if (!is_numeric($slide_down_speed) || $slide_down_speed <= 0 || $slide_down_speed >= 10) {
        $form_state
          ->setErrorByName('lightbox2_slidedown_speed', t('You must enter a duration between 0 and 10 seconds.'));
      }
    }
  }
}