You are here

public function JwplayerPresetAdd::validateForm in JW Player 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/JwplayerPresetAdd.php, line 563

Class

JwplayerPresetAdd
Configure search settings for this site.

Namespace

Drupal\jw_player\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $preset = $this->entity;
  if ($preset
    ->getSetting('preset_source') == 'drupal') {
    if (!$preset
      ->getSetting('width')) {
      $form_state
        ->setErrorByName('settings][width', t('Width field is required.'));
    }
    if ($preset
      ->getSetting('responsive')) {
      if ($preset
        ->getSetting('width') > 100) {
        $form_state
          ->setErrorByName('settings][width', t('Width field must be less than 100%.'));
      }

      // Aspect Ratio Validation.
      if (!$preset
        ->getSetting('aspectratio')) {
        $form_state
          ->setErrorByName('settings][aspectratio', t('Aspect Ratio field is required if Responsive Design is enabled.'));
      }
      elseif (!preg_match('/^([0-9]*):([0-9]*)$/', $preset
        ->getSetting('aspectratio'), $matches) || (!is_numeric($matches[1]) || !is_numeric($matches[2]))) {
        $form_state
          ->setErrorByName('settings][aspectratio', $this
          ->t('Aspect Ratio field must be of the format of two numbers separated by a colon. For example, <em>16:9</em>.'));
      }
    }
    elseif (!$preset
      ->getSetting('height')) {
      $form_state
        ->setErrorByName('settings][height', t('Height field is required.'));
    }
  }
  else {
    preg_match(jw_player_library_url_regex(), $preset
      ->getSetting('player_library_url'), $matches);
    if (!isset($matches[2])) {
      $form_state
        ->setErrorByName('settings][player_library_url', t('Player Library URL does not match format provided by JWPlayer.com.'));
    }
  }
}