You are here

public function SimplesitemapVariantsForm::validateForm in Simple XML sitemap 8.3

@todo Show multiple errors at once. @todo Allow numeric variant names, but bear in mind that they are stored as integer array keys due to how php arrays work.

Overrides FormBase::validateForm

File

src/Form/SimplesitemapVariantsForm.php, line 55

Class

SimplesitemapVariantsForm
Class SimplesitemapVariantsForm @package Drupal\simple_sitemap\Form

Namespace

Drupal\simple_sitemap\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $line = 0;
  $sitemap_types = $this->generator
    ->getSitemapManager()
    ->getSitemapTypes();
  foreach ($this
    ->stringToVariants($form_state
    ->getValue('variants')) as $variant_name => $variant_definition) {
    $placeholders = [
      '@line' => ++$line,
      '@name' => $variant_name,
      '@type' => isset($variant_definition['type']) ? $variant_definition['type'] : '',
      '@label' => isset($variant_definition['label']) ? $variant_definition['label'] : '',
    ];
    if (trim($variant_name) === '') {
      $form_state
        ->setErrorByName('', $this
        ->t("<strong>Line @line</strong>: The variant name cannot be empty.", $placeholders));
    }
    if (!preg_match('/^[\\w\\-_]+$/', $variant_name)) {
      $form_state
        ->setErrorByName('', $this
        ->t("<strong>Line @line</strong>: The variant name <em>@name</em> can only include alphanumeric characters, dashes and underscores.", $placeholders));
    }
    if (is_numeric($variant_name)) {
      $form_state
        ->setErrorByName('', $this
        ->t("<strong>Line @line</strong>: The variant name cannot be numeric.", $placeholders));
    }
    if (!isset($sitemap_types[$variant_definition['type']])) {
      $form_state
        ->setErrorByName('', $this
        ->t("<strong>Line @line</strong>: The variant <em>@name</em> is of a sitemap type <em>@type</em> that does not exist.", $placeholders));
    }
  }
}