You are here

public function FilterFormatFormBase::validateForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/filter/src/FilterFormatFormBase.php \Drupal\filter\FilterFormatFormBase::validateForm()
  2. 10 core/modules/filter/src/FilterFormatFormBase.php \Drupal\filter\FilterFormatFormBase::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

core/modules/filter/src/FilterFormatFormBase.php, line 174

Class

FilterFormatFormBase
Provides a base form for a filter format.

Namespace

Drupal\filter

Code

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

  // @todo Move trimming upstream.
  $format_format = trim($form_state
    ->getValue('format'));
  $format_name = trim($form_state
    ->getValue('name'));

  // Ensure that the values to be saved later are exactly the ones validated.
  $form_state
    ->setValueForElement($form['format'], $format_format);
  $form_state
    ->setValueForElement($form['name'], $format_name);
  $format_exists = $this->entityTypeManager
    ->getStorage('filter_format')
    ->getQuery()
    ->condition('format', $format_format, '<>')
    ->condition('name', $format_name)
    ->execute();
  if ($format_exists) {
    $form_state
      ->setErrorByName('name', $this
      ->t('Text format names must be unique. A format named %name already exists.', [
      '%name' => $format_name,
    ]));
  }
}