You are here

public function RocketChatSettingsForm::validateForm in Rocket.Chat 8.2

Same name and namespace in other branches
  1. 8 src/Form/RocketChatSettingsForm.php \Drupal\rocket_chat\Form\RocketChatSettingsForm::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/RocketChatSettingsForm.php, line 208
Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.

Class

RocketChatSettingsForm
Class RocketChatSettingsForm.

Namespace

Drupal\rocket_chat\Form

Code

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

  // All required fields are submitted.
  if (!empty($form_state
    ->getValue('url'))) {

    // Check if host server is running.
    $smokeCheck = Utility::serverRun($form_state
      ->getValue('url'));
    $info = [];
    if ($smokeCheck) {
      $apiConfig = new Drupal8Config($this
        ->configFactory(), $this->moduleHandler, $this->state, $this->messenger);
      $empty = "";
      $memConfig = new InMemoryConfig($apiConfig, $empty, $empty);
      $memConfig
        ->setElement('rocket_chat_url', $form_state
        ->getValue('url'));
      $apiClient = new ApiClient($memConfig);

      // Check if the Rocket chat is actually functional with an info call.
      $info = $apiClient
        ->info();
    }
    if (!$smokeCheck || !$info['status'] == "OK") {
      $erred = TRUE;
    }
    else {
      $erred = FALSE;
    }
    if ($erred) {
      $form_state
        ->setErrorByName('url', t('Connecting with the RocketChat service failed. Make sure the URL is correct.'));
    }
  }
}