function FilterFormatFormBase::validateForm

Same name and namespace in other branches
  1. 8.9.x core/modules/filter/src/FilterFormatFormBase.php \Drupal\filter\FilterFormatFormBase::validateForm()
  2. 10 core/modules/filter/src/FilterFormatFormBase.php \Drupal\filter\FilterFormatFormBase::validateForm()
  3. 11.x core/modules/filter/src/FilterFormatFormBase.php \Drupal\filter\FilterFormatFormBase::validateForm()

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,
        ]));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.