You are here

public function OptionsLimitWebformHandler::validateOptionsElement in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php \Drupal\webform_options_limit\Plugin\WebformHandler\OptionsLimitWebformHandler::validateOptionsElement()

Validate an options element with limit(s).

@internal This method should only called by OptionsLimitWebformHandler::validateElementLimit.

Parameters

array $element: An options element with limits.

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

File

modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php, line 784

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

public function validateOptionsElement(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state
    ->getFormObject();

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_object
    ->getEntity();
  $this
    ->setWebformSubmission($webform_submission);
  $element_key = $this->configuration['element_key'];

  // Get casting as array to support multiple options.
  $original_values = (array) $webform_submission
    ->getElementOriginalData($element_key);
  $updated_values = (array) $form_state
    ->getValue($element_key);
  if (empty($updated_values) || $updated_values === [
    '',
  ]) {
    return;
  }
  $limits = $this
    ->getOptionsLimits($updated_values);
  foreach ($limits as $value => $limit) {

    // Do not apply option limit to any previously selected option value.
    if (in_array($value, $original_values)) {
      continue;
    }
    if ($limit['status'] === WebformOptionsLimitHandlerInterface::LIMIT_STATUS_NONE) {
      $message = $this
        ->getElementLimitStatusMessage(WebformOptionsLimitHandlerInterface::LIMIT_STATUS_ERROR, $limit);
      $form_state
        ->setError($element, $message);
    }
  }
}