You are here

public function ContentHubSettingsForm::validateForm in Acquia Content Hub 8.2

Same name and namespace in other branches
  1. 8 src/Form/ContentHubSettingsForm.php \Drupal\acquia_contenthub\Form\ContentHubSettingsForm::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/ContentHubSettingsForm.php, line 312

Class

ContentHubSettingsForm
Defines the form to configure the Content Hub connection settings.

Namespace

Drupal\acquia_contenthub\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $action_name = $form_state
    ->getTriggeringElement()['#name'];
  if (!in_array($action_name, [
    'register_site',
    'update_webhook',
    'unregister_site',
  ], TRUE)) {
    return;
  }
  if (!UrlHelper::isValid($form_state
    ->getValue('hostname'), TRUE)) {
    $form_state
      ->setErrorByName('hostname', $this
      ->t('This is not a valid URL. Please insert another one.'));
  }
  $webhook = $this
    ->getFormattedWebhookUrl($form_state);
  if (!UrlHelper::isValid($webhook, TRUE)) {
    $form_state
      ->setErrorByName('webhook', $this
      ->t('Please type a publicly accessible url.'));
  }

  // Important. This should never validate if it is an UUID. Lift 3 does not
  // use UUIDs for the api_key but they are valid for Content Hub.
  $fields = [
    'api_key' => $this
      ->t('Please insert an API Key.'),
    'secret_key' => $this
      ->t('Please insert a Secret Key.'),
    'client_name' => $this
      ->t('Please insert a Client Name.'),
  ];
  foreach ($fields as $field => $error_message) {
    if (!$form_state
      ->hasValue($field)) {
      $form_state
        ->setErrorByName($field, $error_message);
    }
  }
  if (!empty($form_state
    ->getErrors())) {
    return;
  }
  if ($action_name === 'register_site') {
    $this
      ->register($form_state);
  }
}