You are here

public function SolrConnectorPluginBase::validateConfigurationForm in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::validateConfigurationForm()
  2. 8.2 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::validateConfigurationForm()
  3. 4.x src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::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 complete form.

Overrides PluginFormTrait::validateConfigurationForm

See also

\Drupal\Core\Plugin\PluginFormInterface::validateConfigurationForm()

File

src/SolrConnector/SolrConnectorPluginBase.php, line 276

Class

SolrConnectorPluginBase
Defines a base class for Solr connector plugins.

Namespace

Drupal\search_api_solr\SolrConnector

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if (isset($values['port']) && (!is_numeric($values['port']) || $values['port'] < 0 || $values['port'] > 65535)) {
    $form_state
      ->setError($form['port'], $this
      ->t('The port has to be an integer between 0 and 65535.'));
  }
  if (!empty($values['path']) && strpos($values['path'], '/') !== 0) {
    $form_state
      ->setError($form['path'], $this
      ->t('If provided the path has to start with "/".'));
  }
  if (!empty($values['core']) && strpos($values['core'], '/') === 0) {
    $form_state
      ->setError($form['core'], $this
      ->t('Core or collection must not start with "/".'));
  }
  if (!$form_state
    ->hasAnyErrors()) {

    // Try to orchestrate a server link from form values.
    $values_copied = $values;
    $solr = $this
      ->createClient($values_copied);
    $solr
      ->createEndpoint($values_copied + [
      'key' => 'search_api_solr',
    ], TRUE);
    try {
      $this
        ->getServerLink();
    } catch (\InvalidArgumentException $e) {
      foreach ([
        'scheme',
        'host',
        'port',
        'path',
        'core',
      ] as $part) {
        $form_state
          ->setError($form[$part], $this
          ->t('The server link generated from the form values is illegal.'));
      }
    }
  }
}