You are here

public function LiveChatLicenseForm::validateForm in LiveChat 8

Same name and namespace in other branches
  1. 8.2 src/Form/LiveChatLicenseForm.php \Drupal\livechat\Form\LiveChatLicenseForm::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/LiveChatLicenseForm.php, line 97

Class

LiveChatLicenseForm
Configure Coffee for this site.

Namespace

Drupal\livechat\Form

Code

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

  // Validate the license number.
  $login = $form_state
    ->getValue('login');
  if (!empty($login)) {
    try {
      $license_number_request = LIVECHAT_API_LICENSE_NUMBER . '/' . $login;
      $response = $this->httpClient
        ->get($license_number_request);
      $data = $response
        ->getBody()
        ->getContents();
      if (!empty($data)) {
        $json = Json::decode($data);
        if (!empty($json['number'])) {
          if (livechat_validate_license($json['number'])) {
            $form_state
              ->setValue('license_number', $json['number']);
          }
          else {
            $form_state
              ->setErrorByName('login', $this
              ->t('LiveChat license number is incorrect.'));
          }
        }
        elseif (!empty($json['error'])) {
          $form_state
            ->setErrorByName('login', $json['error']);
        }
      }
    } catch (RequestException $e) {
      watchdog_exception('LiveChat', $e, $e
        ->getMessage(), [], RfcLogLevel::ERROR, $license_number_request);
      $form_state
        ->setErrorByName('login', $e
        ->getMessage());
    }
  }
}