You are here

public function SearchApiSolrAcquiaConnector::validateConfigurationForm in Acquia Search 3.x

Same name and namespace in other branches
  1. 2.x src/Plugin/SolrConnector/SearchApiSolrAcquiaConnector.php \Drupal\acquia_search\Plugin\SolrConnector\SearchApiSolrAcquiaConnector::validateConfigurationForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides SolrConnectorPluginBase::validateConfigurationForm

File

src/Plugin/SolrConnector/SearchApiSolrAcquiaConnector.php, line 272

Class

SearchApiSolrAcquiaConnector
Class SearchApiSolrAcquiaConnector.

Namespace

Drupal\acquia_search\Plugin\SolrConnector

Code

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

  // Remove whitespaces.
  foreach ([
    'identifier',
    'uuid',
    'api_key',
    'api_host',
  ] as $key) {
    $form_state
      ->setValue($key, trim($form_state
      ->getValue($key)));
  }

  // No trailing slash allowed for a API host.
  $form_state
    ->setValue('api_host', rtrim($form_state
    ->getValue('api_host'), '/'));
  $values = $form_state
    ->getValues();
  if (!preg_match('@^[A-Z]{4,5}-[0-9]{5,6}$@', $values['identifier'])) {
    $form_state
      ->setErrorByName('identifier', $this
      ->t('Enter a valid identifier.'));
  }
  if (!preg_match('@^(https?://|)[a-z0-9\\.-]*$@', $values['api_host'])) {
    $form_state
      ->setErrorByName('api_host', $this
      ->t('Enter a valid domain.'));
  }
  if (!preg_match('@^[0-9a-f-]*$@', $values['uuid'])) {
    $form_state
      ->setErrorByName('uuid', $this
      ->t('Enter a valid UUID.'));
  }
}