You are here

public function IndexStatusForm::validateForm in Search API 8

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/IndexStatusForm.php, line 207

Class

IndexStatusForm
Provides a form for indexing, clearing, etc., an index.

Namespace

Drupal\search_api\Form

Code

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

  // Only the "Index now" action needs any validation.
  if ($form_state
    ->getTriggeringElement()['#name'] === 'index_now') {
    $values = $form_state
      ->getValues();

    // Get the translated "all" value and lowercase it for comparison.
    $all_value = mb_strtolower($values['all']);
    foreach ([
      'limit',
      'batch_size',
    ] as $field) {

      // Trim and lowercase the value so we correctly identify "all" values,
      // even if not matching exactly.
      $value = mb_strtolower(trim($values[$field]));
      if ($value === $all_value) {
        $value = -1;
      }
      elseif (!$value || !is_numeric($value) || (int) $value != $value) {
        $form_state
          ->setErrorByName($field, $this
          ->t('Enter a non-zero integer. Use "-1" or "@all" for "all items".', [
          '@all' => $values['all'],
        ]));
      }
      else {
        $value = (int) $value;
      }
      $form_state
        ->setValue($field, $value);
    }
  }
}