You are here

public function AddCustomPlaceForm::validateForm in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Form/AddCustomPlaceForm.php \Drupal\weather\Form\AddCustomPlaceForm::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/AddCustomPlaceForm.php, line 176

Class

AddCustomPlaceForm
Configure Weather settings for this site.

Namespace

Drupal\weather\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Remove whitespaces.
  $url = trim($form_state
    ->getValue('weather_yrno_url'));

  // Check for the english version.
  if (substr($url, 0, 24) != 'https://www.yr.no/place/') {
    $form_state
      ->setErrorByName('weather_yrno_url', $this
      ->t('Please make sure to use the English version of the forecast, starting with "https://www.yr.no/<strong>place</strong>/".'));
  }
  list($country, $link) = $this->weatherHelper
    ->parsePlaceUrl($url);
  $placeExists = $this->entityTypeManager
    ->getStorage('weather_place')
    ->getQuery()
    ->condition('country', $country)
    ->condition('link', $link)
    ->execute();
  if ($placeExists) {
    $form_state
      ->setErrorByName('weather_yrno_url', $this
      ->t('The place is already in the database'));
  }
}