You are here

public static function WebformValidateConstraint::validateFrontEqualComponent in Webform Validation 8

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

Validates Equal components on front end.

Parameters

array $element: The form element to process.

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

array $form: The form array.

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

File

src/Validate/WebformValidateConstraint.php, line 98

Class

WebformValidateConstraint
Form API callback. Validate element value.

Namespace

Drupal\webform_validation\Validate

Code

public static function validateFrontEqualComponent(array &$element, FormStateInterface $formState, array &$form) : void {
  $webformKey = $element['#webform_key'];
  $equalComponents = $element['#equal_components'];
  $thisValue = is_array($formState
    ->getValue($webformKey)) ? $formState
    ->getValue($webformKey) : [
    $formState
      ->getValue($webformKey),
  ];
  $submittedValues = $formState
    ->cleanValues()
    ->getValues();
  $storage = $formState
    ->getStorage();
  $visitedElements = !empty($storage['visited']) ? $storage['visited'] : [];
  $error = FALSE;
  foreach ($equalComponents as $key => $value) {

    // Equal component key.
    if (isset($form['elements'][$key])) {
      $fieldElement = $form['elements'][$key];
      $fieldElement['access'] = !empty($form['elements'][$key]['#access']);
      $fieldElement['multiple'] = !empty($form['elements'][$key]['#webform_multiple']);
    }
    elseif (!isset($form['elements'][$key])) {
      $found = FALSE;
      $fieldElement = [];
      self::getFormElementAccess($form['elements'], $key, $found, $fieldElement);
    }
    if (!empty($fieldElement['access']) && !in_array($key, $visitedElements)) {
      $visitedElements[] = $key;
    }
    if (!empty($element['#access']) && !in_array($webformKey, $visitedElements)) {
      $visitedElements[] = $webformKey;
    }

    // Equal component validation.
    if (!empty($thisValue[0]) && !empty($submittedValues[$key]) && array_key_exists($key, $submittedValues) && in_array($key, $visitedElements) && in_array($webformKey, $visitedElements)) {

      // Many to Many.
      if (!empty($fieldElement['multiple']) && $element['#webform_multiple'] == TRUE) {
        $result = array_intersect($thisValue, $submittedValues[$key]);
        if (empty($result) && !empty($submittedValues[$key])) {
          $error = TRUE;
          break;
        }
      }
      elseif (!empty($fieldElement['multiple'])) {
        if (!in_array($thisValue[0], $submittedValues[$key])) {
          $error = TRUE;
          break;
        }
      }
      elseif (!in_array($submittedValues[$key], $thisValue) && !empty($element['#webform_multiple'])) {
        $error = TRUE;
        break;
      }
      elseif (!in_array($submittedValues[$key], $thisValue) && empty($element['#webform_multiple']) && empty($fieldElement['multiple'])) {
        $error = TRUE;
        break;
      }
    }
    elseif ($value != '0' && !empty($thisValue[0]) && in_array($key, $visitedElements) && empty($submittedValues[$key])) {
      $error = TRUE;
      break;
    }
    elseif ($value != '0' && empty($thisValue[0]) && !empty($submittedValues[$key]) && in_array($webformKey, $visitedElements)) {
      $error = TRUE;
      break;
    }
  }
  $storage['visited'] = $visitedElements;
  $formState
    ->setStorage($storage);
  if ($error) {
    if (empty($fieldElement['access']) || !empty($element['#access']) && empty($thisValue[0])) {
      $fieldElement = $element;
      $content = "should not be Empty";
    }
    if (isset($fieldElement['#title'])) {
      $tArgs = [
        '%name' => empty($fieldElement['#title']) ? $fieldElement['#parents'][0] : $fieldElement['#title'],
        '%value' => $value,
        '%content' => !empty($content) ? $content : 'does not match',
        '%sourceName' => empty($content) ? empty($element['#title']) ? $element['#parents'][0] : $element['#title'] : '',
      ];
      $formState
        ->setError($fieldElement, t('%name %content %sourceName', $tArgs));
    }
    else {
      $formState
        ->setError($element);
    }
  }
}