You are here

public function CredentialForm::validateForm in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 src/Form/CredentialForm.php \Drupal\acquia_connector\Form\CredentialForm::validateForm()
  2. 3.x src/Form/CredentialForm.php \Drupal\acquia_connector\Form\CredentialForm::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/CredentialForm.php, line 104

Class

CredentialForm
Form for Acquia Credentials.

Namespace

Drupal\acquia_connector\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  try {
    $response = $this->client
      ->nspiCall('/agent-api/subscription', [
      'identifier' => trim($form_state
        ->getValue('acquia_identifier')),
    ], trim($form_state
      ->getValue('acquia_key')));
  } catch (ConnectorException $e) {

    // Set form error to prevent switching to the next page.
    if ($e
      ->isCustomized()) {

      // Allow to connect with expired subscription.
      if ($e
        ->getCustomMessage('code') == Subscription::EXPIRED) {
        $form_state
          ->setValue('subscription', 'Expired subscription.');
        return;
      }
      acquia_connector_report_restapi_error($e
        ->getCustomMessage('code'), $e
        ->getCustomMessage());
      $form_state
        ->setErrorByName('');
    }
    else {
      $form_state
        ->setErrorByName('', $this
        ->t('Server error, please submit again.'));
    }
    return;
  }
  $response = $response['result'];
  if (empty($response['body']['subscription_name'])) {
    $form_state
      ->setErrorByName('acquia_identifier', $this
      ->t('No subscriptions were found.'));
  }
  else {
    $form_state
      ->setValue('subscription', $response['body']['subscription_name']);
  }
}