You are here

public function LiveWeatherForm::validateForm in Live Weather 8

Same name and namespace in other branches
  1. 8.2 src/Form/LiveWeatherForm.php \Drupal\live_weather\Form\LiveWeatherForm::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/LiveWeatherForm.php, line 87
Contains \Drupal\live_weather\Form\LiveWeatherForm.

Class

LiveWeatherForm
Controller location for Live Weather Form.

Namespace

Drupal\live_weather\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $locations = $this->configFactory
    ->get('live_weather.location')
    ->get('location');
  $location_value = $form_state
    ->getValue('location');
  if (empty($location_value) || !is_numeric($location_value)) {
    $form_state
      ->setErrorByName('location', $this
      ->t('location invalid.'));
  }
  elseif (!empty($locations) && array_key_exists($location_value, $locations)) {
    $form_state
      ->setErrorByName('location', $this
      ->t('location already exists.'));
  }
  elseif (!empty($location_value)) {
    $output = $this->liveWeather
      ->locationCheck($location_value, 'location', 'f');
    if (!empty($output)) {
      if (isset($output['query']['results']['channel']['location']) && !empty($output['query']['results']['channel']['location'])) {
        $city = $output['query']['results']['channel']['location']['city'] . ',' . $output['query']['results']['channel']['location']['region'] . ',' . $output['query']['results']['channel']['location']['country'];
        $locations[$location_value] = $city;
        $this
          ->config('live_weather.location')
          ->set('location', $locations)
          ->save();
      }
      else {
        $form_state
          ->setErrorByName('location', $this
          ->t('location invalid.'));
      }
    }
  }
}