You are here

public static function YamlFormMultiple::validateYamlFormMultiple in YAML Form 8

Validates form list element.

File

src/Element/YamlFormMultiple.php, line 406

Class

YamlFormMultiple
Provides a form element to assist in creation of multiple elements.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormMultiple(&$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\yamlform\Element\YamlFormEmailConfirm::validateYamlFormEmailConfirm
  // @see \Drupal\yamlform\Element\YamlFormOtherBase::validateYamlFormOther
  $values = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);

  // Convert values to items.
  $items = self::convertValuesToItems($element, $values['items']);

  // Validate required items.
  if (!empty($element['#required']) && empty($items)) {
    if (isset($element['#required_error'])) {
      $form_state
        ->setError($element, $element['#required_error']);
    }
    elseif (isset($element['#title'])) {
      $form_state
        ->setError($element, t('@name field is required.', [
        '@name' => $element['#title'],
      ]));
    }
    else {
      $form_state
        ->setError($element);
    }
    return;
  }
  $form_state
    ->setValueForElement($element, $items);
}