You are here

public static function YamlFormOptions::validateYamlFormOptions in YAML Form 8

Validates form options element.

File

src/Element/YamlFormOptions.php, line 114

Class

YamlFormOptions
Provides a form element to assist in creation of options.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormOptions(&$element, FormStateInterface $form_state, &$complete_form) {
  $options_value = NestedArray::getValue($form_state
    ->getValues(), $element['options']['#parents']);
  if (is_string($options_value)) {
    $options = Yaml::decode($options_value);
  }
  else {
    $options = self::convertValuesToOptions($options_value);
  }

  // Validate required options.
  if (!empty($element['#required']) && empty($options)) {
    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, $options);
}