You are here

public static function WebformMultiple::validateWebformMultiple in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformMultiple.php \Drupal\webform\Element\WebformMultiple::validateWebformMultiple()

Validates webform multiple element.

1 call to WebformMultiple::validateWebformMultiple()
WebformSubmissionViews::validateWebformMultiple in src/Element/WebformSubmissionViews.php
Validates webform multiple element.
1 method overrides WebformMultiple::validateWebformMultiple()
WebformSubmissionViews::validateWebformMultiple in src/Element/WebformSubmissionViews.php
Validates webform multiple element.

File

src/Element/WebformMultiple.php, line 916

Class

WebformMultiple
Provides a webform element to assist in creation of multiple elements.

Namespace

Drupal\webform\Element

Code

public static function validateWebformMultiple(&$element, FormStateInterface $form_state, &$complete_form) {

  // IMPORTANT: Must get values from the $form_states since sub-elements
  // may call $form_state->setValueForElement() via their validation hook.
  // @see \Drupal\webform\Element\WebformEmailConfirm::validateWebformEmailConfirm
  // @see \Drupal\webform\Element\WebformOtherBase::validateWebformOther
  $values = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);
  $number_of_items_storage_key = static::getStorageKey($element, 'number_of_items');
  $number_of_items = $form_state
    ->get($number_of_items_storage_key);
  if (!empty($values['items']) && ($number_of_items || $element['#cardinality'])) {
    $items = $values['items'];

    // Validate unique keys.
    if ($error_message = static::validateUniqueKeys($element, $items)) {
      $form_state
        ->setError($element, $error_message);
      return;
    }

    // Convert values to items and validate duplicate keys.
    $items = static::convertValuesToItems($element, $items);

    // Validate required items.
    if (!empty($element['#required']) && empty($items)) {
      WebformElementHelper::setRequiredError($element, $form_state);
    }
  }
  else {
    $items = [];
  }
  $element['#value'] = $items;
  $form_state
    ->setValueForElement($element, $items);
}