You are here

public function AutocompletionConfigurationFormBase::validateForm in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Form/AutocompletionConfigurationFormBase.php \Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::validateForm()

Overrides Drupal\Core\Entity\EntityFormController::validate().

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

Overrides FormBase::validateForm

File

src/Form/AutocompletionConfigurationFormBase.php, line 132

Class

AutocompletionConfigurationFormBase
Class AutocompletionConfigurationFormBase.

Namespace

Drupal\search_autocomplete\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Retrieve all configurations with same selector.
  $entities = $this->entityStorage
    ->loadByProperties([
    'selector' => $form_state
      ->getValue('selector'),
  ]);

  // If other configurations have the same selector (not null)...
  if ($entities != NULL && $form_state
    ->getValue('selector')) {

    // Exclude that same entity (case of update).
    if (count($entities) == 1 && isset($entities[$this->entity
      ->id()])) {
      return;
    }
    else {
      $form_state
        ->setErrorByName('selector', $this
        ->t('The selector ID must be unique.'));
    }
  }
}