You are here

public function BrightcoveAPIClientForm::validateForm in Brightcove Video Connect 8.2

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

Class

BrightcoveAPIClientForm
Class BrightcoveAPIClientForm.

Namespace

Drupal\brightcove\Form

Code

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

    // Try to authorize client and save values on success.
    $client = Client::authorize($form_state
      ->getValue('client_id'), $form_state
      ->getValue('secret_key'));

    // Test account ID.
    $cms = new CMS($client, $form_state
      ->getValue('account_id'));
    $cms
      ->countVideos();
    $this->keyValueExpirableStore
      ->setWithExpire($form_state
      ->getValue('id'), $client
      ->getAccessToken(), intval($client
      ->getExpiresIn()) - 30);
  } catch (AuthenticationException $e) {
    $form_state
      ->setErrorByName('client_id', $e
      ->getMessage());
    $form_state
      ->setErrorByName('secret_key');
  } catch (APIException $e) {
    $form_state
      ->setErrorByName('account_id', $e
      ->getMessage());
  }
}