You are here

public function JavascriptLibrariesCustomAddForm::validateForm in JavaScript Libraries Manager 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/JavascriptLibrariesCustomAddForm.php, line 118
Contains \Drupal\javascript_libraries\Form\JavascriptLibrariesCustomAddForm.

Class

JavascriptLibrariesCustomAddForm

Namespace

Drupal\javascript_libraries\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  switch ($form_state
    ->getValue('library_type')) {
    case 'external':
      if (strlen($form_state
        ->getValue('external_url')) == 0) {
        $form_state
          ->setErrorByName('external_url', $this
          ->t("An empty URL is invalid."));
      }
      elseif (!javascript_libraries_valid_external_url($form_state
        ->getValue('external_url'))) {
        $form_state
          ->setErrorByName('external_url', $this
          ->t('This URL does not start with http:// or does not end with ".js" or ".txt".'));
      }
      break;
    case 'file':
      if (empty($form_state
        ->getValue('js_file_upload'))) {
        $form_state
          ->setErrorByName('js_file_upload', $this
          ->t("File field is required when adding a file."));
      }
      break;
  }
}