You are here

public function TermLevelItem::validateLevels in Term Level Field 8

Validates levels value.

File

src/Plugin/Field/FieldType/TermLevelItem.php, line 105

Class

TermLevelItem
Plugin for Term level field type.

Namespace

Drupal\term_level\Plugin\Field\FieldType

Code

public function validateLevels($element, FormStateInterface $form_state) {
  $levels = self::extractLevels($element['#value']);
  if (count($levels) == 0) {
    $form_state
      ->setError($element, $this
      ->t('Please enter valid levels.'));
  }
  foreach ($levels as $key => $label) {
    if (!preg_match('/^\\d+$/', $key)) {
      $form_state
        ->setError($element, $this
        ->t('The level key must be positive integer.'));
      break;
    }
  }
}