You are here

public function ConfigurationForm::validateForm in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x src/Form/ConfigurationForm.php \Drupal\pwa\Form\ConfigurationForm::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/ConfigurationForm.php, line 320

Class

ConfigurationForm
Class ConfigurationForm.

Namespace

Drupal\pwa\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $default_image = $form_state
    ->getValue('default_image');
  $img = $form_state
    ->getValue([
    'image',
    0,
  ]);
  $config = $this
    ->config('pwa.config');
  if ($config
    ->get('default_image') && !$default_image && !isset($img)) {
    $form_state
      ->setErrorByName('image', $this
      ->t('Upload a image, or chose the theme image.'));
  }

  // Check urls format
  $urls_to_cache = pwa_str_to_list($form_state
    ->getValue('urls_to_cache'));
  foreach ($urls_to_cache as $page) {

    // If link is internal.
    try {
      $url = Url::fromUserInput($page);
    } catch (\Exception $e) {
      $form_state
        ->setErrorByName('urls_to_cache', $this
        ->t("The user-entered URL '{$page}' must begin with a '/', '?', or '#'."));
    }

    // If link does not exist.
    if (isset($url) && !$url
      ->isRouted()) {
      $form_state
        ->setErrorByName('urls_to_cache', $this
        ->t('Error "' . $page . '" URL to Cache is a 404.'));
    }
  }
}