You are here

public static function WebformCodeMirror::validateWebformCodeMirror in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformCodeMirror.php \Drupal\webform\Element\WebformCodeMirror::validateWebformCodeMirror()

Webform element validation handler for #type 'webform_codemirror'.

File

src/Element/WebformCodeMirror.php, line 141

Class

WebformCodeMirror
Provides a webform element for using CodeMirror.

Namespace

Drupal\webform\Element

Code

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

  // If element is disabled then use the #default_value.
  if (!empty($element['#disable'])) {
    $element['#value'] = $element['#default_value'];
    $form_state
      ->setValueForElement($element, $element['#default_value']);
  }
  $errors = static::getErrors($element, $form_state, $complete_form);
  if ($errors) {
    $build = [
      'title' => [
        '#markup' => t('%title is not valid.', [
          '%title' => static::getTitle($element),
        ]),
      ],
      'errors' => [
        '#theme' => 'item_list',
        '#items' => $errors,
      ],
    ];
    $form_state
      ->setError($element, \Drupal::service('renderer')
      ->render($build));
  }
  else {

    // If editing YAML and #default_value is an array, decode #value.
    if ($element['#mode'] === 'yaml' && (isset($element['#default_value']) && is_array($element['#default_value']) || $element['#decode_value'])) {
      $value = $element['#value'] ? Yaml::decode($element['#value']) : [];
      $form_state
        ->setValueForElement($element, $value);
    }
  }
}