You are here

public static function WebformValidateConstraint::validateFrontSomeSeveralComponent in Webform Validation 8

Same name and namespace in other branches
  1. 2.0.x src/Validate/WebformValidateConstraint.php \Drupal\webform_validation\Validate\WebformValidateConstraint::validateFrontSomeSeveralComponent()

Validates Some of Several in fields.

Parameters

array $element: The form elements.

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

array $form: The form array.

1 call to WebformValidateConstraint::validateFrontSomeSeveralComponent()
WebformValidateConstraint::validateElements in src/Validate/WebformValidateConstraint.php
Validates element.

File

src/Validate/WebformValidateConstraint.php, line 273

Class

WebformValidateConstraint
Form API callback. Validate element value.

Namespace

Drupal\webform_validation\Validate

Code

public static function validateFrontSomeSeveralComponent(array &$element, FormStateInterface $formState, array &$form) : void {
  $flatValues = array_filter($formState
    ->cleanValues()
    ->getValues());
  $formObject = $formState
    ->getFormObject();
  $webform = $formObject
    ->getWebform();
  $storage = $formState
    ->getStorage();
  $webformKey = $element['#webform_key'];
  $sosComponents = array_filter($element['#some_of_several_components']);
  $sosCompletedComponents = $element['#some_of_several_components_completed'];
  $sosFinalValidation = $element['#some_of_several_final_validation'];
  $performValidation = TRUE;
  if (!empty($sosFinalValidation)) {
    $pages = $webform
      ->getPages();
    if (!empty($pages)) {
      $currentPage = $formState
        ->get('current_page');
      $nextPage = WebformArrayHelper::getNextKey($pages, $currentPage);
      if ($nextPage != 'webform_confirmation') {
        $performValidation = FALSE;
      }
    }
  }
  unset($flatValues['submit']);
  $currentArray = [
    $webformKey => $webformKey,
  ];
  $sosComponentsRev = array_merge_recursive($sosComponents, $currentArray);
  $items = [];
  $itemsfound = 0;
  $visitedElements = !empty($storage['visited']) ? $storage['visited'] : [];
  foreach ($sosComponentsRev as $cid => $component) {
    if (in_array($cid, $visitedElements)) {
      if (!empty($flatValues[$cid])) {
        $items[$cid] = $flatValues[$cid];
      }
      $itemsfound++;
    }
    else {
      $found = $flag = FALSE;
      $fieldElement = [];
      self::getFormElementAccess($form['elements'], $cid, $found, $fieldElement, $flag);
      if (!empty($fieldElement['access'])) {
        if (!empty($flatValues[$cid])) {
          $items[$cid] = $flatValues[$cid];
        }
        $visitedElements[] = $cid;
        $itemsfound++;
      }
    }
  }
  $storage['visited'] = $visitedElements;
  $formState
    ->setStorage($storage);
  $compareNumber = (int) preg_replace('/[^0-9]+/', '', $element['#some_of_several_components_completed']);
  if ($compareNumber < 1) {
    $compareNumber = 1;
  }
  elseif ($compareNumber > count($sosComponentsRev)) {
    $compareNumber = count($sosComponentsRev);
  }
  $validationFlag = FALSE;
  if (substr($sosCompletedComponents, 0, 1) === '=') {
    if ($itemsfound >= $compareNumber) {
      $validationFlag = TRUE;
    }
  }
  elseif (substr($sosCompletedComponents, 0, 1) === '<=') {
    if ($itemsfound > 0) {
      $validationFlag = TRUE;
    }
  }
  else {
    if ($itemsfound >= $compareNumber) {
      $validationFlag = TRUE;
    }
  }
  if ($validationFlag && $performValidation) {
    $numberCompleted = count($items);
    $severalComponents = [];
    $elements = $webform
      ->getElementsInitializedAndFlattened();
    foreach ($elements as $elementKey => &$elementComponent) {
      if (in_array($elementComponent['#webform_key'], $sosComponentsRev)) {
        $severalComponents[$elementKey] = $elementComponent['#admin_title'];
      }
    }

    // Parse the comparision operator and do comparison.
    $error = FALSE;
    if (substr($sosCompletedComponents, 0, 1) === '=') {
      if (!($numberCompleted === $compareNumber)) {
        $verb = t('exactly');
        $error = TRUE;
      }
    }
    elseif (substr($sosCompletedComponents, 0, 2) === '<=') {
      if (!($numberCompleted <= $compareNumber)) {
        $verb = t('at most');
        $error = TRUE;
      }
    }
    else {
      if (!($numberCompleted >= $compareNumber)) {
        $verb = t('at least');
        $error = TRUE;
      }
    }
    if ($error) {
      $renderable = [
        '#theme' => 'item_list',
        '#items' => $severalComponents,
      ];
      $items = \Drupal::service('renderer')
        ->render($renderable);
      $errorMessage = t('You must complete %verb %compare_number of these items: %items', [
        '%verb' => $verb,
        '%compare_number' => $compareNumber,
        '%items' => $items,
      ]);
      $formState
        ->setError($element, $errorMessage);
    }
  }
}