You are here

public static function OptionsShsWidget::validateElement in Simple hierarchical select 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldWidget/OptionsShsWidget.php \Drupal\shs\Plugin\Field\FieldWidget\OptionsShsWidget::validateElement()

Form validation handler for widget elements.

Parameters

array $element: The form element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Overrides OptionsWidgetBase::validateElement

File

src/Plugin/Field/FieldWidget/OptionsShsWidget.php, line 266

Class

OptionsShsWidget
Plugin implementation of the 'options_shs' widget.

Namespace

Drupal\shs\Plugin\Field\FieldWidget

Code

public static function validateElement(array $element, FormStateInterface $form_state) {
  parent::validateElement($element, $form_state);
  if (empty($element['#shs']['settings']['force_deepest']) || $form_state
    ->hasAnyErrors()) {
    return;
  }
  $value = $element['#value'];
  if (!is_array($value)) {
    $value = [
      $value,
    ];
  }
  if ($element['#shs']['settings']['anyValue'] === reset($value)) {
    if (!$element['#required']) {
      return;
    }
    elseif (count($element['#options']) > 1) {
      $form_state
        ->setError($element, t('You need to select a term from the deepest level in field @name.', [
        '@name' => $element['#title'],
      ]));
      return;
    }
  }
  foreach ($value as $element_value) {
    if (shs_term_has_children($element_value)) {
      $form_state
        ->setError($element, t('You need to select a term from the deepest level in field @name.', [
        '@name' => $element['#title'],
      ]));
      return;
    }
  }
}