You are here

public static function ShsTermSelect::validateForceDeepest in Webform Simple Hierarchical Select 8

Form API callback. Validate the force deepest option.

Parameters

array $element: The element.

FormStateInterface $form_state: The form state.

File

src/Element/ShsTermSelect.php, line 116

Class

ShsTermSelect
Provides a webform element for an shs term select menu.

Namespace

Drupal\webform_shs\Element

Code

public static function validateForceDeepest(array &$element, FormStateInterface $form_state) {
  if (empty($element['#force_deepest'])) {
    return;
  }
  if (!empty($element['#force_deepest_error'])) {
    $message = $element['#force_deepest_error'];
  }
  else {
    $message = t('You need to select a term from the deepest level in field @name.', [
      '@name' => $element['#title'],
    ]);
  }
  $value = $form_state
    ->getValue($element['#name']);
  if (!is_array($value)) {
    $value = [
      $value,
    ];
  }
  foreach ($value as $element_value) {

    // If nothing was selected.
    if ($element['#shs']['settings']['anyValue'] === $element_value) {

      // Skip this value row and check the next one.
      if (!$element['#required']) {
        continue;
      }
      elseif (count($element['#options']) > 1) {
        $form_state
          ->setError($element, $message);
        return;
      }
    }
    elseif (shs_term_has_children($element_value)) {
      $form_state
        ->setError($element, $message);
      return;
    }
  }
}