You are here

protected function YamlFormEntityElementsValidator::validateDuplicateNames in YAML Form 8

Validate elements does not contain duplicate names.

Return value

array|null If not valid, an array of error messages.

1 call to YamlFormEntityElementsValidator::validateDuplicateNames()
YamlFormEntityElementsValidator::validate in src/YamlFormEntityElementsValidator.php
Validate form elements.

File

src/YamlFormEntityElementsValidator.php, line 160

Class

YamlFormEntityElementsValidator
Defines a class to validate form elements.

Namespace

Drupal\yamlform

Code

protected function validateDuplicateNames() {
  $duplicate_names = [];
  $this
    ->getDuplicateNamesRecursive($this->elements, $duplicate_names);
  if ($duplicate_names = array_filter($duplicate_names)) {
    $messages = [];
    foreach ($duplicate_names as $duplicate_name => $duplicate_count) {
      $line_numbers = $this
        ->getLineNumbers('/^\\s*(["\']?)' . preg_quote($duplicate_name, '/') . '\\1\\s*:/');
      $t_args = [
        '%name' => $duplicate_name,
        '@lines' => $this
          ->formatPlural(count($line_numbers), $this
          ->t('line'), $this
          ->t('lines')),
        '@line_numbers' => YamlFormArrayHelper::toString($line_numbers),
      ];
      $messages[] = $this
        ->t('Elements contain a duplicate element name %name found on @lines @line_numbers.', $t_args);
    }
    return $messages;
  }
  return NULL;
}