You are here

public static function YamlFormElementOptions::validateYamlFormElementOptions in YAML Form 8

Validates a form element options element.

File

src/Element/YamlFormElementOptions.php, line 146

Class

YamlFormElementOptions
Provides a form element for managing form element options.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormElementOptions(&$element, FormStateInterface $form_state, &$complete_form) {
  $options_value = NestedArray::getValue($form_state
    ->getValues(), $element['options']['#parents']);
  $custom_value = NestedArray::getValue($form_state
    ->getValues(), $element['custom']['#parents']);
  $value = $options_value;
  if ($options_value == self::CUSTOM_OPTION) {
    try {
      $value = is_string($custom_value) ? Yaml::decode($custom_value) : $custom_value;
    } catch (\Exception $exception) {

      // Do nothing since the 'yamlform_codemirror' element will have already
      // captured the validation error.
    }
  }
  $has_access = !isset($element['#access']) || $element['#access'] === TRUE;
  if ($element['#required'] && empty($value) && $has_access) {
    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);
    }
  }
  $form_state
    ->setValueForElement($element['options'], NULL);
  $form_state
    ->setValueForElement($element['custom'], NULL);
  $form_state
    ->setValueForElement($element, $value);
}